我有一个
ComboBox
这需要取决于另一个人的价值
. 这一部分已经工作,与依赖
组合框
组合框
:
<!-- Independent -->
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="2"
x:Name="cbo_product" VerticalAlignment="Center" Width="120"
ItemsSource="{Binding Source={StaticResource productsXml}}"
DisplayMemberPath="@name" SelectedValuePath="@name"
SelectionChanged="cbo_product_SelectionChanged"
SelectedValue="{Binding Path=Product}" />
<!-- Dependent -->
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="2"
x:Name="cbo_component" VerticalAlignment="Center" Width="201"
DataContext="{Binding SelectedItem, ElementName=cbo_product}"
ItemsSource="{Binding XPath=Components/Component}"
DisplayMemberPath="@name" SelectedValuePath="@name"
SelectedValue="{Binding Path=Component}"
SelectionChanged="cbo_component_SelectionChanged" />
在这背后的C班,我有:
public MyUserControlConstructor()
{
MyViewModelInstance= new MyViewModel();
DataContext = MyViewModelInstance;
}
而且在
MyViewModel
,我有:
public string Component
{
get { return _component; }
set
{
if (value == _component)
{
return;
}
_component = value;
onPropertyChanged(PropertyNames.Component);
}
}
private void onPropertyChanged(PropertyNames fieldName)
{
if (null == PropertyChanged)
{
return;
}
string propertyName = Enum.GetName(typeof(PropertyNames), fieldName);
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
当我改变了相依关系
组合框
(组件),当然,它会在我的应用程序中显示新值。但是,当我按下一个按钮,导致
Component
属性,它始终是初始值,而不是我刚才在
组合框
. 我想我的XAML中肯定有错误。对于C#,我试着遵循
this
和
this guide
组合框
嵌套在
SelectedItem
组合框
,但仍然更新
组成部分
编辑:
我的怀疑是因为我设定了
DataContext
对于家属
在两个地方:第一个在C#的构造函数中,指向我的视图模型,第二个在XAML中,指向
DataContext="{Binding SelectedItem, ElementName=cbo_product}"
.
编辑:
我在视图模型类的构造函数中设置了初始值。当我取出
属性,则即使在我更改依赖
,我仍然没有从中得到任何价值
组成部分
财产。这几乎是我已经知道的:依赖者
组合框
与《独立报》有关
组合框
组合框
组成部分
财产。
根据要求,以下是我的XML示例:
<Products xmlns="">
<Product name="Awesomeness">
<Components>
<Component name="Component of Glory"/>
<Component name="Component of Doom"/>
</Components>
</Product>
</Products>
编辑:
我猜是
MultiBinding
看了以后会有用的
this
和
this
.
编辑:
看来我应该可以得到家属了
不固定工作
数据上下文
,只需使用
ItemsSource
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="2"
x:Name="cbo_component" VerticalAlignment="Center" Width="201"
ItemsSource="{Binding ElementName=cbo_product, Path=SelectedItem,
XPath=Components/Component}"
DisplayMemberPath="@name" SelectedValuePath="@name"
SelectedValue="{Binding Path=Component}"
SelectionChanged="cbo_component_SelectionChanged"/>
然而,这是行不通的:依赖者
组合框