代码之家  ›  专栏  ›  技术社区  ›  Sarah Vessels

WPF,绑定到依赖组合框的属性总是给出初始值

  •  1
  • Sarah Vessels  · 技术社区  · 14 年前

    我有一个 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"/>
    

    然而,这是行不通的:依赖者 组合框

    1 回复  |  直到 8 年前
        1
  •  0
  •   Sarah Vessels    14 年前

    我发现解决这个问题的方法是在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 组合框 在XAML中绑定,并用在依赖项中选择的值填充 组合框 因为我不需要换衣服 DataContext 家属的 .