c# - 带有两个项目源的组合框

标签 c# wpf combobox datatrigger itemssource

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

由于单独的列表与不变的组合框一起工作,我已经知道这不是数据上下文类型的问题。我还安装了 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#( View 模型):

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; }
}

最佳答案

代替

<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 的初始分配移动到样式 setter 中:

<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 .


另一个问题是你绑定(bind)到 View 中特定元素的样式,这使得它可重用,你看到你必须创建与你拥有的单选按钮一样多的数据触发器.此外,您在绑定(bind)中使用了错误的 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 的类),它将用于运行触发器。

或者更简单的是更改绑定(bind)到 View 模型中的 ItemsSource 的单个属性,这样您就不需要 View 中的触发器。

关于c# - 带有两个项目源的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47197811/

相关文章:

c# - 在 IFrame 中的 ASP.NET Core 2.0 应用程序中使用 session 状态

c# - 如何查找字符串上的所有 img 标签?

WPF Dockpanel 第一个子项使用剩余空间

c# - ComboBox.SelectedIndexChanging?

java - 如何加载未选择任何值的枚举填充的组合框

php - 从选定的数据库中选择选项值

c# - 如何使用 Facebook 应用程序开发界面邀请用户连接到您的应用程序

c# - 使用 WPF 绘制 3D 线

c# - 在 Canvas 中放置对象

c# - 无法捕获 sleep /暂停消息 (winXP)