[英]Set initial value in Combobox bound to CollectionViewSource
[英]WPF, property bound to dependent ComboBox always gives initial value
我有一个ComboBox
,它需要依赖于另一个ComboBox
的值。 在独立的ComboBox
选择新值时,此部分已经起作用,并且依赖的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));
}
当然,当我更改依赖的ComboBox
(组件)时,它会在我的应用程序中显示新值。 但是,当我按下导致显示Component
属性值的按钮时,它始终是初始值,而不是我刚刚在ComboBox
选择的值。 我认为XAML中肯定有错误。 对于C#,我试图遵循的组合这个和本指南 。 如何将我的从属ComboBox
绑定到嵌套在独立ComboBox
的SelectedItem
中的XML值,但仍更新类中的Component
属性?
编辑:我的怀疑是,事情很古怪,因为我在两个地方设置了依赖ComboBox
的DataContext
:首先在C#的构造函数中,设置为我的视图模型,第二在XAML中,设置为DataContext="{Binding SelectedItem, ElementName=cbo_product}"
。
编辑:我已经在构造函数中为我的视图模型类设置了初始值。 当我取出Component
属性的初始值时,即使更改了相关ComboBox
的选定值,我仍然无法从Component
属性中得到任何值。 这几乎可以改写我已经知道的内容:依赖的ComboBox
绑定到独立的ComboBox
(它从独立的ComboBox
获取数据),而不是Component
属性。
编辑:根据要求,这是我的XML示例:
<Products xmlns="">
<Product name="Awesomeness">
<Components>
<Component name="Component of Glory"/>
<Component name="Component of Doom"/>
</Components>
</Product>
</Products>
编辑:在查看this和this之后,我猜想MultiBinding
是有用的。
编辑:似乎我应该能够通过使用ItemsSource
而不设置DataContext
来使依赖的ComboBox
工作:
<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"/>
但是,这不起作用:依赖的ComboBox
为空,而不是显示所有组件名称。
我发现解决此问题的方法涉及在C#中设置ItemsSource而不是XAML,我不希望这样做。 但是,它起作用了,经过一天半的努力,这是我想到的最好的方法。
在XAML中:
<!-- 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"
SelectedItem="{Binding Path=ProductNode}"
SelectedValue="{Binding Path=Product}" />
<!-- Dependent -->
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="2"
x:Name="cbo_component" VerticalAlignment="Center" Width="201"
DisplayMemberPath="@name" SelectedValuePath="@name"
SelectedValue="{Binding Path=Component, Mode=TwoWay}"
SelectionChanged="cbo_component_SelectionChanged"/>
在C#中,独立ComboBox
发生更改时的事件处理程序:
private void cbo_product_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
// Set ItemsSource of dependent ComboBox
cbo_component.ItemsSource = getChildNodesFromComboBox(
sender as ComboBox, "Components/Component"
);
cbo_component.SelectedIndex = 0;
}
// Helper method to do XPath query and get child nodes from selected value of
// independent ComboBox
private XmlNodeList getChildNodesFromComboBox(ComboBox comboBox,
string xpath)
{
if (null == comboBox)
{
return null;
}
var xml = comboBox.SelectedItem as XmlElement;
if (null == xml)
{
return null;
}
return xml.SelectNodes(xpath);
}
现在,我的视图模型类中的依赖项(在XAML中绑定到我的依赖ComboBox
)的Component
属性填充了在依赖ComboBox
选择的值,因为我不必更改依赖ComboBox
的DataContext
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.