【发布时间】:2021-01-23 23:44:48
【问题描述】:
我有一个ListBox,项目是动态生成的。我还有一个ComboBox。
每个项目都有一个名为“名称”的string 属性。
当我在ListBox 上选择此项目时,我想将ComboBox 的默认值或文本设置为ListBox 中所选项目的“名称”。
我可以将它与“TextBlock”的“Text”属性绑定。但我无法使用ComboBox
这是我的代码:
XAML
<ListBox Name="MyList" ItemsSource="{Binding SourceList}"
SelectedItem="{Binding SelectedObject, Mode=TwoWay}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding ElementName=MyList, Path=ActualWidth}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<StackPanel>
<Grid>
<StackPanel>
<TextBlock {Binding Path=FileName}" />
<TextBlock>
<Run Text="{Binding Name}" />
</TextBlock>
</StackPanel>
</Grid>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我可以绑定这个TextBlock:
<TextBlock>
<Run Text="{Binding SelectedItem.Name, ElementName=MyList}"/>
</TextBlock>
但它不适用于ComboBox:
<ComboBox ItemsSource="{Binding ElementName=MyList, Path=SelectedItem}" SelectedItem="{Binding SelectedObject}" Text="{Binding Name}">
</ComboBox>
【问题讨论】:
-
ComboBox.ItemsSource是用于填充组合框项目的枚举。 SelectedItem 可能不是可枚举的,或者?
标签: c# .net wpf combobox binding