Combobox有两个itemssources

时间:2017-11-09 09:10:30

标签: c# wpf combobox datatrigger itemssource

我试图根据单选按钮列出客户或供应商的列表。当我将一个列表绑定到组合框时,它可以工作。当我尝试使用数据触发器动态更改要显示的集合时,没有任何反应。

由于单独的列表适用于不变的组合框,我已经知道它不是datacontext类型的问题。我还安装了Fody Weaver来自动创建INotifyProperties。

XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <RadioButton Content="Klant"
                 Grid.Column="0"
                 Foreground="White"
                 Margin="10"
                 IsChecked="{Binding ButtonCustomerIsChecked}"
                 GroupName="CustomerSupplier"
                 Name="RadioButtonCustomer"
                 />
    <RadioButton Content="Leverancier"
                 Grid.Column="1"
                 Foreground="White"
                 Margin="10"
                 IsChecked="{Binding ButtonSupplierIsChecked}"
                 GroupName="CustomerSupplier"
                 Name="RadioButtonSupplier"
                 />
</Grid>

<ComboBox ItemsSource="{Binding SupplierList}"
          DisplayMemberPath="Name"
          SelectedItem="{Binding Path=SelectedCustomerSupplier}"
          Style="{StaticResource InputComboBox}"
          />

<ComboBox Style="{StaticResource InputComboBox}"
          ItemsSource="{Binding}"
          DisplayMemberPath="Name"
          >
    <Style TargetType="{x:Type ComboBox}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsChecked, ElementName=ButtonCustomerIsChecked, diag:PresentationTraceSources.TraceLevel=High}"  Value="True">
                <Setter Property="ItemsSource" Value="{Binding CustomerList}"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding IsChecked, ElementName=ButtonSupplierIsChecked, diag:PresentationTraceSources.TraceLevel=High}" Value="True">
                <Setter Property="ItemsSource" Value="{Binding SupplierList}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ComboBox>

C#(ViewModel):

CustomerList = new ObservableCollection<Customer>
{
    new Customer{Id=1, Name="Bart"},
    new Customer{Id=1, Name="Nick"},
    new Customer{Id=1, Name="Erwin"},
};

SupplierList = new ObservableCollection<Customer>
{
    new Customer{Id=1, Name="Rita"},
    new Customer{Id=1, Name="Sascha"},
    new Customer{Id=1, Name="Didier"},
};

更多C#(客户类):

public class Customer : IStakeholder
{
    /// <summary>
    /// The ID of the customer
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// The name of the customer
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// The phone number
    /// </summary>
    public string PhNr { get; set; }

    /// <summary>
    /// The VAT number
    /// can be null
    /// </summary>
    public string VatNr { get; set; }

    /// <summary>
    /// The country where the customer is located
    /// </summary>
    public CountryCat CountryCat { get; set; }

    /// <summary>
    /// A list of payments made by the customer
    /// </summary>
    public List<IPayments> Payments { get; set; }
}

1 个答案:

答案 0 :(得分:2)

而不是

<ComboBox ItemsSource="{Binding SupplierList}" ...>
    <ComboBox.Style>
        <Style TargetType="{x:Type ComboBox}">
            <Style.Triggers>
                <DataTrigger ...>
                    <Setter Property="ItemsSource" Value="{Binding CustomerList}"/>
                </DataTrigger>
            </Style.Triggers>
        <Style>
    <ComboBox.Style>
</ComboBox>

您必须将ItemsSource的初始分配移动到样式设置器中,如下所示:

<ComboBox ...>
    <ComboBox.Style>
        <Style TargetType="{x:Type ComboBox}">
            <Style.Setters>
                <Setter Property="ItemsSource" Value="{Binding SupplierList}" />
            </Style.Setters>
            <Style.Triggers>
                <DataTrigger ...>
                    <Setter Property="ItemsSource" Value="{Binding CustomerList}"/>
                </DataTrigger>
            </Style.Triggers>
        <Style>
    <ComboBox.Style>
</ComboBox>

否则样式触发器无法覆盖本地值,请参阅dependency property value precedence

另一个问题是你在视图中绑定到特定元素的样式,这使得它不那么可重用,你会看到你需要创建尽可能多的数据触发器,你有多少个无线电按钮。此外,您在绑定中使用了错误的ElementName,例如它不是ButtonCustomerIsChecked,应该是RadioButtonCustomer(供应商也一样)。

这是一个修复,你只需要一个触发器:

 <Style.Triggers>
     <DataTrigger Binding="{Binding IsChecked, ElementName=RadioButtonCustomer}" Value="True">
         <Setter Property="ItemsSource" Value="{Binding CustomerList}"/>
     </DataTrigger>
 </Style.Triggers>

更好的方法是在ViewModel中提供一个属性(它的MVVM术语,viewmodel是一个定义了CustomerList的类),它将用于运行触发器。

甚至更容易更改视图模型中绑定到ItemsSource的单个属性,这样您就不需要在视图中使用触发器。