所以,我有一个模型
public class MyModel
{
public string Id { get; }
public string Representation { get; }
public static IEnumerable<MyModel> MyModels() { ... }
}
使用MyModels作为ItemSource的组合框,其中MyModel.Representation定义为显示的属性
<ObjectDataProvider x:Key="myModelsData"
ObjectType="{x:Type models:MyModel}"
MethodName="MyModels"/>
<ComboBox Name="MyBox"
ItemsSource="{Binding Source={StaticResource myModelsData}}"
DisplayMemberPath="Representation"/>
现在,我正在进行多绑定,我想引用的不是MyModel.Representation,而是指MyModel.Id。怎么做?
我现在知道的唯一方法是
<Button Command="{Binding MyCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource myModelConverter }">
<Binding ElementName="MyBox" Path="Text"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
但Text包含MyModel.representation。那么,怎么样?
<Binding Path={MyBox.SelectedItem.Id}/>
答案 0 :(得分:0)
假设您有ViewModel
,那么您可以将ComboBox.SelectedItem
绑定到某个媒体资源,然后从那里访问您的Model
。
class ViewModel
{
public MyModel SelectedModel { get; set; }
public ICommand MyCommand => new DelegateCommand(MyCommandMethod);
public void MyCommandMethod()
{
string representation = SelectedModel.Representation;
}
}
然后在你的View
..
<ComboBox Name="MyBox"
ItemsSource="{Binding Source={StaticResource myModelsData}}"
DisplayMemberPath="Representation"
SelectedItem="{Binding SelectedModel}"/>
<Button Command="{Binding MyCommand}" />