WPF,绑定到依赖ComboBox的属性始终给出初始值

时间:2010-08-13 18:13:46

标签: c# wpf data-binding xaml combobox

我有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#,我尝试按照thisthis guide的组合进行操作。如何将我的从属ComboBox绑定到嵌套在独立SelectedItem的{​​{1}}中的XML值,但仍然更新我班级中的ComboBox属性?

编辑:我怀疑是因为我在两个地方为依赖Component设置了DataContext:首先在C#的构造函数中,我的观点模型,XAML中的第二个,ComboBox

编辑:我一直在构造函数中将初始值设置为我的视图模型类。当我取出DataContext="{Binding SelectedItem, ElementName=cbo_product}"属性的初始值时,即使我更改了从属Component中的选定值,我仍然没有从ComboBox属性中获取任何值。这几乎只是重新解释了我已经知道的内容:依赖Component与独立ComboBox绑定(它从独立的ComboBox获取数据,即),但不是{ {1}}属性。

编辑按要求,这是我的XML样本:

ComboBox

修改:在查看thisthis后,我猜测Component会有用。

编辑:好像我应该可以让相关的<Products xmlns=""> <Product name="Awesomeness"> <Components> <Component name="Component of Glory"/> <Component name="Component of Doom"/> </Components> </Product> </Products> 无法设置MultiBinding,只需使用ComboBox

DataContext

但是,这不起作用:从属ItemsSource为空,而不是显示所有组件名称。

1 个答案:

答案 0 :(得分:0)

我找到解决这个问题的方法涉及在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);
}

现在,我的视图模型类中的Component属性(我的依赖项ComboBox在XAML中绑定)将填充在从属ComboBox中选择的值,因为我没有必须更改从属DataContext的{​​{1}}。

相关问题