【发布时间】:2026-01-08 09:55:02
【问题描述】:
我正在尝试根据单选按钮列出客户或供应商。当我将一个列表绑定到组合框时,它可以工作。当我尝试使用数据触发器动态更改要显示的集合时,没有任何反应。
由于单独的列表与不变的组合框一起使用,我已经知道这不是数据上下文类型的问题。我还安装了 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#(视图模型):
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; }
}
【问题讨论】:
-
您更有可能遇到value precedence 问题,将初始属性分配移动到样式设置器中。
-
您能详细说明一下吗?我对 XAML/C# 还很陌生,我不确定您指的是哪个初始属性分配。
标签: c# wpf combobox datatrigger itemssource