0

我是 WPF 的新手。I have a combobox which when choosing a value three other fields ( AbbrBlock, MultiBrandSupplier, IgnoreNoCompetition) update along to show the correct relevant values according to the data source. 这没问题。

当我尝试向组合框添加自定义值时出现问题,尽管组合框正确显示所有值,但在更改组合框的值时其他字段不会改变。

这是工作代码(没有额外的自定义组合框值 - 剥离到关键部分):

<Window.Resources>
    <local:OrdersDataSet x:Key="ordersDataSet" />
    <CollectionViewSource x:Key="caSuppliersViewSource" Source="{Binding CaSuppliers, Source={StaticResource ordersDataSet}}"/>
</Window.Resources>

...

<StackPanel DataContext="{StaticResource caSuppliersViewSource}">
    <ComboBox Name="SupplierDropdown" DisplayMemberPath="SupplierName" 
        ItemsSource="{Binding Source={StaticResource ResourceKey=caSuppliersViewSource}}"/>
    <TextBlock Name="AbbrBlock" VerticalAlignment="Center" Text="{Binding Abbr}"/>
    <CheckBox Name="MultiBrandSupplier" IsChecked="{Binding MultiBrand}"/>
    <CheckBox Name="IgnoreNoCompetition" IsChecked="{Binding IgnoreNoCompetition}"/>
</StackPanel>

这是添加了正确显示的自定义值的代码,但在更改组合框值时其他字段不会更新:

<Window.Resources>
    <local:OrdersDataSet x:Key="ordersDataSet" />
    <CollectionViewSource x:Key="caSuppliersViewSource" Source="{Binding CaSuppliers, Source={StaticResource ordersDataSet}}"/>
</Window.Resources>

...

<StackPanel DataContext="{StaticResource caSuppliersViewSource}">
    <StackPanel.Resources>
        <CompositeCollection x:Key="myCompositeCollection">
            <CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=caSuppliersViewSource}}" />
            <ComboBoxItem Content="Add New..." />
        </CompositeCollection>
    </StackPanel.Resources>
    <ComboBox Name="SupplierDropdown" DisplayMemberPath="SupplierName" 
        ItemsSource="{Binding Source={StaticResource myCompositeCollection}}"/>
    <TextBlock Name="AbbrBlock" VerticalAlignment="Center" Text="{Binding Abbr}"/>
    <CheckBox Name="MultiBrandSupplier" IsChecked="{Binding MultiBrand}"/>
    <CheckBox Name="IgnoreNoCompetition" IsChecked="{Binding IgnoreNoCompetition}"/>
</StackPanel>

我在这里想念什么?

4

1 回答 1

1

看起来ComboBox正在更新caSuppliersViewSourceView.CurrentItem属性(我认为)以匹配它SelectedItem在您的第一个片段中。在第二种情况下,CollectionViewSourcea 被埋在里面,CompositeCollection这样就不会再发生了。但是,ComboBox它仍在选择一个项目,您可以使用 绑定到该项目ElementName。此版本无需设置DataContextStackPanel

<StackPanel>
    <StackPanel.Resources>
        <CompositeCollection x:Key="myCompositeCollection">
            <CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=caSuppliersViewSource}}" />
            <ComboBoxItem Content="Add New..." />
        </CompositeCollection>
    </StackPanel.Resources>
    <ComboBox 
        Name="SupplierDropdown" 
        DisplayMemberPath="SupplierName" 
        ItemsSource="{Binding Source={StaticResource myCompositeCollection}}"
        />
    <TextBlock 
        Name="AbbrBlock" 
        VerticalAlignment="Center" 
        Text="{Binding SelectedItem.Abbr, ElementName=SupplierDropdown}"
        />
    <CheckBox 
        Name="MultiBrandSupplier" 
        IsChecked="{Binding SelectedItem.MultiBrand, ElementName=SupplierDropdown}"
        />
    <CheckBox 
        Name="IgnoreNoCompetition" 
        IsChecked="{Binding SelectedItem.IgnoreNoCompetition, ElementName=SupplierDropdown}"
        />
</StackPanel>

您还可以为您的视图模型提供与包含SelectedDBItem的任何内容相同类型的属性caSuppliersViewSource,并绑定ComboBox.SelectedItem到该属性。然后你可以这样做:

    <TextBlock 
        Name="AbbrBlock" 
        VerticalAlignment="Center" 
        Text="{Binding SelectedDBItem}"
        />

但那是六打一,另一半,或其他东西——除非你想SelectedDBItem在你的视图模型中做其他事情,那么它很方便。

于 2016-07-12T19:34:22.683 回答