2017-11-09 10 views
2

ラジオボタンに基づいて顧客またはサプライヤのリストを作成しようとしています。 1つのリストをコンボボックスにバインドすると、そのコンボボックスが機能します。データトリガーを使用して表示するコレクションを動的に変更しようとすると、何も起こりません。2つのアイテムソースを持つコンボボックス

別のリストは変更されないコンボボックスで動作するので、私はすでにそれがデータ型の問題ではないことを知っています。私は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#(Customerクラス):

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

[価値優先](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence)の問題に遭遇し、初期プロパティの割り当てをスタイルセッター。 – Sinatr

+0

詳細を教えてください。私はXAML/C#をかなり新しくしています。そして、あなたが意味する最初のプロパティの割り当てがわかりません。 –

答えて

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> 
それ以外の場合は スタイルのトリガーdependency property value precedenceを参照してください、 ローカル値を上書きすることはできません

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

:あなたはこのようなスタイルのセッターにItemsSourceの初期割り当てを移動する必要が


もう一つの問題は、スタイルであなたは、これはそれがあまり再利用可能なは、あなたができるだけ多くのデータを作成しなければならないものを見ますあなたが持っているなど、多くのラジオボタンをトリガ、ビュー内の特定の要素に結合されているものです。また、そこにはバインドに間違ったElementNameが使用されています。それはではなく、RadioButtonCustomer(サプライヤと同じ)でなければなりません。される(それがMVVM用語だ、のviewmodelあなたはCustomerListが定義されているクラスである)、より良いアプローチはViewModelにでプロパティを提供することである

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

:ここ

が修正され、1つだけのトリガーを必要としますトリガーを実行するために使用されます。

さらに、viewmodelでItemsSourceにバインドされた単一のプロパティを変更する方が簡単です。この方法では、ビューでトリガーは必要ありません。

+2

''は冗長です。 – Clemens

+0

お返事ありがとうございます。私は自分のコードを更新しましたが、私はあなたのの間に設定されている最初のリストのみを取得します。だから私がラジオボタンの1つを押すと、リストはまだ変わらない。 –

+1

@クレメンス、ありがとう、wpf/facepalmで3年間働いておいてよかったです。 – Sinatr

関連する問題