我有一个组合框,它具有绑定到观察值集合的项源,并使用Data-template作为项模板。我在组合框.value上单击时遇到一个问题,它没有在组合框中显示所选值的值
下面是我的数据模板:
<DataTemplate x:Key="ComboBoxTemplate">
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding ColorCode}" Width="30" Height="15"/>
<TextBlock Text="{Binding ColorName}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>下面是我的组合框:
<ComboBox Name="cmbAccentColors" Grid.Column="1" Width="130" Height="20"
ItemsSource="{Binding Source={StaticResource ComboColorData}}"
ItemTemplate="{StaticResource ComboBoxTemplate}"
IsSynchronizedWithCurrentItem="True" MaxDropDownHeight="120"
SelectionChanged="cmbColors_SelectionChanged"
>
Private void cmbColors_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (!IsLoaded)
{
return;
}
MessageBox.Show(cmbAccentColors.SelectedItem.ToString());
}发布于 2014-05-15 15:20:23
将DisplayMemberPath添加到您的组合框。DisplayMemberPath指定路径。
DisplayMemberPath = "YourProperty";
这也是您访问所选项目的方式,
Private void cmbColors_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string text = (sender as ComboBox).SelectedItem.Text;
}https://stackoverflow.com/questions/23671595
复制相似问题