2017-07-07 51 views
0

從不止一個組合框中選擇相同的值我有一個簡單的類這樣:如何防止用戶在WPF

public class Item 
{ 
    public int ID{get;set;} 
    public string Name{get;set;} 
} 

我有這個類在我Mainwindow.xaml.cs這樣一個名單:

public List<Item> AllItems=GetAllItems(); 

我有項目類的四個屬性在我Mainwindow.xaml.cs這樣:

public Item Item1{get;set;} 
public Item Item2{get;set;} 
public Item Item3{get;set;} 
public Item Item4{get;set;} 

此列表:AllIte MS是databinded四個組合框爲下:

<ComboBox x:Name="cmbCode1" ItemsSource="{Binding AllItems}" DisplayMemberPath="ID" SelectedValuePath="ID" SelectedValue="{Binding Item1.ID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Item1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 
<ComboBox x:Name="cmbCode2" ItemsSource="{Binding AllItems}" DisplayMemberPath="ID" SelectedValuePath="ID" SelectedValue="{Binding Item1.ID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Item1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 
<ComboBox x:Name="cmbCode3" ItemsSource="{Binding AllItems}" DisplayMemberPath="ID" SelectedValuePath="ID" SelectedValue="{Binding Item1.ID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Item1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 
<ComboBox x:Name="cmbCode4" ItemsSource="{Binding AllItems}" DisplayMemberPath="ID" SelectedValuePath="ID" SelectedValue="{Binding Item1.ID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Item1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 

我具有對應於這四個組合框4個文本框爲這樣:

<TextBlock x:Name="txtName1" Text="{Binding Item1.Name,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/> 
<TextBlock x:Name="txtName2" Text="{Binding Item2.Name,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/> 
<TextBlock x:Name="txtName3" Text="{Binding Item3.Name,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/> 
<TextBlock x:Name="txtName4" Text="{Binding Item4.Name,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/> 

我要的是用戶不應該能夠選擇相同來自多個組合框的ID。 是否有一些簡單的方法可以完成,特別是僅使用xaml?我如何隱藏或顯示從其他組合框中選擇/取消選擇的項目,以便用戶無法從多個組合框中選擇相同的ID? 到目前爲止,我已經嘗試將選定的項目和整個列表發送到多值轉換器,並將項目清除/添加到列表本身,但這似乎太meesy.Any其他更好的主意,將不勝感激。

+0

XAML是*標記*語言...它並不意味着被用來實現這種邏輯。你應該使用編程語言來做到這一點。 – mm8

+0

@ mm8:目前爲止,我對WPF有限的理解,我目前正在使用多值轉換器,其中我發送的不是一個,而是全部四個列表,每個SelectedItem發生更改,並且正在執行所有正負列表。那裏的更新現在我不會想知道它是否可能在XAML中,我不知道.. :-) .. –

回答

0

您可以爲每個組合框單獨列表。然後,您可以爲其中的每個添加LostFocus事件處理程序。您可以使用它重新填充其他ComboBoxes的列表以排除選擇。

例如,如果我的列表中有5個項目;最初我可以在我的任何ComboBox中選擇全部5個。當我在ComboBox1中選擇Item1時,LostFocus事件處理程序將更新組合框2-4後面的列表以刪除Item1。當我然後在ComboBox2中選擇Item2時,LostFocus事件處理程序將更新ComboBoxes 3和4後面的列表以刪除Item2。等等...

另一種方法可能是讓用戶選擇他們喜歡的任何東西,然後對選定的值運行某種驗證以確保它們是唯一的。這article通過您的一些選項。

就我個人而言,我會採用第二種方法;也許在文本框上方顯示一條消息,指示選擇必須是唯一的。您可以指出任何錯誤並阻止依賴於選擇的任何操作,但無需持續更新數據,這可能會導致更平滑的用戶界面。

0

您可以使用代碼隱藏在不同的組合框中

for (int count = 0; count <= cmb1.Items.Count -1; count++) 
    { 

    if((ComboBoxItem)(cmb1.Items[count])).SelectedValue==TextBox1.Text) 
((ComboBoxItem)(cmb1.Items[count])).Visibility = System.Windows.Visibility.Collapsed; 
     } 

此代碼,你可以在comboxes選定事件中寫入所選項目。

我想你可以使用觸發器在XAML

0

您應該處理這種邏輯在您的視圖模型編寫相同的邏輯。下面是一個例子,應該給你的想法:

視圖模型:

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
     AllItems = new List<Item>() { new Item { Name = "1" }, new Item { Name = "2" }, new Item { Name = "3" }, new Item { Name = "4" } }; 
    } 

    public List<Item> AllItems { get; set; } 

    private Item _item1; 
    public Item Item1 
    { 
     get { return _item1; } 
     set 
     { 
      _item1 = value; 
      NotifyPropertyChanged(); 
      if (value != null) 
       value.CanSelect = false; 
     } 
    } 

    private Item _item2; 
    public Item Item2 
    { 
     get { return _item2; } 
     set 
     { 
      _item2 = value; 
      NotifyPropertyChanged(); 
      if (value != null) 
       value.CanSelect = false; 
     } 
    } 

    private Item _item3; 
    public Item Item3 
    { 
     get { return _item3; } 
     set 
     { 
      _item3 = value; NotifyPropertyChanged(); 
      if (value != null) 
       value.CanSelect = false; 
     } 
    } 

    private Item _item4; 
    public Item Item4 
    { 
     get { return _item4; } 
     set 
     { 
      _item4 = value; 
      NotifyPropertyChanged(); 
      if (value != null) 
       value.CanSelect = false; 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 

    } 
} 

public class Item : INotifyPropertyChanged 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 

    private bool _canSelect = true; 
    public bool CanSelect 
    { 
     get { return _canSelect; } 
     set { _canSelect = value; NotifyPropertyChanged(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

XAML:

<Style x:Key="icstyle" TargetType="ComboBoxItem"> 
    <Setter Property="IsEnabled" Value="{Binding CanSelect}" /> 
</Style> 
... 
<ComboBox x:Name="cmbCode1" ItemsSource="{Binding AllItems}" SelectedItem="{Binding Item1}" DisplayMemberPath="Name" ItemContainerStyle="{StaticResource icstyle}"/> 
<ComboBox x:Name="cmbCode2" ItemsSource="{Binding AllItems}" SelectedItem="{Binding Item2}" DisplayMemberPath="Name" ItemContainerStyle="{StaticResource icstyle}"/> 
<ComboBox x:Name="cmbCode3" ItemsSource="{Binding AllItems}" SelectedItem="{Binding Item3}" DisplayMemberPath="Name" ItemContainerStyle="{StaticResource icstyle}"/> 
<ComboBox x:Name="cmbCode4" ItemsSource="{Binding AllItems}" SelectedItem="{Binding Item4}" DisplayMemberPath="Name" ItemContainerStyle="{StaticResource icstyle}"/>