Gui*_*ira 8 c# wpf listbox mvvm visual-studio-2010
我遇到了以下问题:我有两个ListBox,有两个不同ItemSource,但两个都有相同binding的SelectedItem,因为我试图在这两个列表之间执行单一选择.
这是一个更好地显示问题的图像:

我想做什么?每次我从第一个列表中的一个项目(红色),应该取消了SelectedItem从第二个列表(黑色),反之亦然.这就是我binding为他们两个使用相同的原因.我真的不知道这是否是更好的方法,但它应该像那样工作.
你可以帮帮我吗?
sa_*_*213 13
尝试使用SelectedValue,这将使你看到的行为变得复杂
<ListBox SelectedValue="{Binding MySelectedItem}" />
Run Code Online (Sandbox Code Playgroud)
似乎SelectedItem没有取消选择是在列表中找不到所选项目,但SelectedValue似乎确实取消选择它,不知道为什么
您可以在此示例应用中看到差异:
XAML:
<Window x:Class="WpfApplication11.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="184" Width="208" x:Name="UI">
<StackPanel DataContext="{Binding ElementName=UI}">
<TextBlock Text="SelectedValue" />
<StackPanel Orientation="Horizontal" Height="60" >
<ListBox ItemsSource="{Binding MyItemSource1}" SelectedValue="{Binding MySelectedValue}" Width="100" />
<ListBox ItemsSource="{Binding MyItemSource2}" SelectedValue="{Binding MySelectedValue}" Width="100" />
</StackPanel>
<TextBlock Text="SelectedItem" />
<StackPanel Orientation="Horizontal" Height="60" >
<ListBox ItemsSource="{Binding MyItemSource1}" SelectedItem="{Binding MySelectedItem}" Width="100" />
<ListBox ItemsSource="{Binding MyItemSource2}" SelectedItem="{Binding MySelectedItem}" Width="100" />
</StackPanel>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
码:
public partial class MainWindow : Window , INotifyPropertyChanged
{
private CustomObject _mySelectedItem;
private CustomObject _mySelectedValue;
private ObservableCollection<CustomObject> _items = new ObservableCollection<CustomObject>();
private ObservableCollection<CustomObject> _items2 = new ObservableCollection<CustomObject>();
public MainWindow()
{
InitializeComponent();
MyItemSource1.Add(new CustomObject { Name = "Stack" });
MyItemSource1.Add(new CustomObject { Name = "Overflow" });
MyItemSource2.Add(new CustomObject { Name = "Stack" });
MyItemSource2.Add(new CustomObject { Name = "Overflow" });
}
public ObservableCollection<CustomObject> MyItemSource1
{
get { return _items; }
set { _items = value; }
}
public ObservableCollection<CustomObject> MyItemSource2
{
get { return _items2; }
set { _items2 = value; }
}
public CustomObject MySelectedItem
{
get { return _mySelectedItem; }
set { _mySelectedItem = value; NotifyPropertyChanged("MySelectedItem"); }
}
public CustomObject MySelectedValue
{
get { return _mySelectedValue; }
set { _mySelectedValue = value; NotifyPropertyChanged("MySelectedValue"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public class CustomObject
{
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
Run Code Online (Sandbox Code Playgroud)

我要做的是首先传递null给属性并通知更改,然后将实际值传递给属性并将更改通知给视图。像那样:
protected Bar selectedItem;
public Bar SelectedItem{
get
{
return selectedItem;
}
set
{
selectedItem = null;
NotifyPropertyChanged("SelectedItem");
selectedItem = value;
NotifyPropertyChanged("SelectedItem");
}
Run Code Online (Sandbox Code Playgroud)
我从这个问题中得到了这个答案和例子。
| 归档时间: |
|
| 查看次数: |
3525 次 |
| 最近记录: |