[英]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.