带有两个项源的组合框

3
我正在尝试根据单选按钮制作客户或供应商列表。当我将一个列表绑定到组合框时,它可以正常工作。但是当我尝试使用数据触发器动态更改要显示的集合时,什么也没有发生。
由于单独的列表与不变的组合框一起使用时可以工作,所以我已经知道这不是数据上下文类型的问题。我还安装了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; }
}

更有可能遇到值优先级问题,将初始属性赋值移入样式设置器中。 - Sinatr
你能详细说明一下吗?我对XAML/C#还比较新,不确定你指的是哪个初始属性赋值。 - Bart Pinnock
1个回答

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>

否则,样式触发器无法覆盖本地值,请参见依赖属性值优先级
另一个问题是您绑定到视图中特定元素的样式,这使其不够可重用,您需要创建与单选按钮数量相同的数据触发器。此外,您在绑定中使用了错误的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的单个属性,这样您就不需要在视图中使用触发器。

2
<Style.Setters> 是多余的。 - Clemens

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接