当你拥有它时:
SelectedItem="{Binding Path=Name}"
它将使用现在在组合框中选择的内容,Name 的类属性被用作 Selected。如果没有路径,您将绑定到该组合框名称对象。但是无论如何,这在您使用 Path 的情况下不应该起作用。所以要让它按你的意愿工作,试试这个:
将 SelectedItem 绑定到 CharacterEntity 类:
SelectedItem="{Binding SelectedEntity}" // Class instance of CharacterEntity
然后你有一个 Text 绑定到 Name 的选定实体类属性:
Text="{Binding Path=Name}" // Binded to property of Name
SelectedValue="{Binding Path=Tag}" // Binded to property of Tag
这样它应该可以工作。您应该有一个绑定到视图模型的组合框,并且该视图模型应该具有 SelectedEntity 的属性(CharacterEntity 的类实例)。希望这会有所帮助:
public class CharacterViewModel
{
public CharacterEntity SelectedEntity {get;set;}
public List<CharacterEntity> characters {get;set;} // use ObservableCollection insteand of List(Automatically update UI if list changes)
}
还有 XAML:
<ComboBox x:Name="cmbCharacters1" HorizontalAlignment="Left" Margin="18,21,0,0" VerticalAlignment="Top" Width="136" SelectedIndex="0" Height="32" RenderTransformOrigin="1.53,-1.281"
ItemsSource="{Binding characters}" Text="{Binding Path=Name}" SelectedItem="{Binding SelectedEntity}" SelectedValue="{Binding Path=Tag}"/>
在代码隐藏中也有,例如在构造函数中:
CharacterViewModel charViewModel = new CharacterViewModel();
cmdCharacters1.DataContext = charViewModel;
cmdCharacters1.ItemsSource= charViewModel.characters;
我很难解释这一点,但我希望它对我的代码有意义。