簡體   English   中英

WPF,綁定到相關ComboBox的屬性始終提供初始值

[英]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綁定到嵌套在獨立ComboBoxSelectedItem中的XML值,但仍更新類中的Component屬性?

編輯:我的懷疑是,事情很古怪,因為我在兩個地方設置了依賴ComboBoxDataContext :首先在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>

編輯:在查看thisthis之后,我猜想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選擇的值,因為我不必更改依賴ComboBoxDataContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM