wpf - 跨不同窗口中不同控件的 wpf 绑定同步
问题描述
我有三个窗口,我需要在其中同步一个值。我需要跨三个窗口更新所选项目。其中两个包含一个组合框和一个包含标签的主窗口。因此,我需要两个组合框相同,并且标签会更新以反映组合框中的选定项目。
目前我同步了这两个组合,但我的问题是如何同步标签。我试过使用属性,使用 DependencyProperties,实施 INotifyPropertyChanged 但标签没有运气
我有这个示例代码无法同步标签:
主窗口.cs:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public CollectionView Items { get; set; }
private string _selectedItem;
public event PropertyChangedEventHandler PropertyChanged;
public string SelectedItem
{
get { return (string)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem",
typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public MainWindow()
{
DataContext = this;
InitializeComponent();
Left = 100; Width = 350; Height = 200; Top = 10;
Business business = new Business();
Items = new CollectionView(business.GetItemsFromWebService());
Wnd1 wnd1 = new Wnd1();
wnd1.Left = 100; wnd1.Width = 350; wnd1.Height = 200; wnd1.Top = 210;
wnd1.Show();
Wnd2 wnd2 = new Wnd2();
wnd2.Left = 100; wnd2.Width = 350; wnd2.Height = 200; wnd2.Top = 410;
wnd2.Show();
wnd1.cbItems.ItemsSource = Items;
wnd1.cbItems.SelectedValue = SelectedItem;
wnd2.cbItems.ItemsSource = Items;
wnd2.cbItems.SelectedValue = SelectedItem;
Binding labelBinding = new Binding();
labelBinding.Mode = BindingMode.TwoWay;
labelBinding.Source = SelectedItem;
labelBinding.Path = new PropertyPath("SelectedItem");
lbSelected.SetBinding(Label.ContentProperty, labelBinding);
Binding cmbBindingW1 = new Binding();
cmbBindingW1.Mode = BindingMode.TwoWay;
cmbBindingW1.Source = SelectedItem;
cmbBindingW1.Path = new PropertyPath("SelectedItem");
wnd1.cbItems.SetBinding(ComboBox.SelectedItemProperty, cmbBindingW1);
wnd1.cbItems.SelectionChanged += CbItems_SelectionChanged;
Binding cmbBindingW2 = new Binding();
cmbBindingW2.Mode = BindingMode.TwoWay;
cmbBindingW2.Source = SelectedItem;
cmbBindingW2.Path = new PropertyPath("SelectedItem");
wnd1.cbItems.SetBinding(ComboBox.SelectedItemProperty, cmbBindingW2);
}
private void CbItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int a = 0;
SelectedItem = ((ComboBox)sender).SelectedItem.ToString();
}
}
MainWindow.xaml:
<Window x:Class="DropDownSync.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DropDownSync"
mc:Ignorable="d"
Title="MainWindow" Height="183.333" Width="376.667"
Name="wMain">
<Grid>
<Label Name="lbSelected" HorizontalAlignment="Left" Margin="10,71,0,0" VerticalAlignment="Top" Width="80"
Visibility="Visible"/>
</Grid>
</Window>
商业.cs:
public class Business
{
public List<string> GetItemsFromWebService()
{
List<string> result = new List<string>();
result.Add("item1");
result.Add("item2");
result.Add("item3");
return result;
}
}
Wnd1.xaml:
<Window x:Class="DropDownSync.Wnd1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DropDownSync"
mc:Ignorable="d"
Title="Wnd1" Height="211.667" Width="518.333"
Name="w1">
<Grid>
<ComboBox Name="cbItems" HorizontalAlignment="Left" Margin="90,14,0,0" VerticalAlignment="Top" Width="120"/>
<Label Content="Current item" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="80"/>
</Grid>
</Window>
Wnd2.xaml:
<Window x:Class="DropDownSync.Wnd2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DropDownSync"
mc:Ignorable="d"
Title="Wnd2" Height="190" Width="453.333"
Name="w2">
<Grid>
<ComboBox Name="cbItems" HorizontalAlignment="Left" Margin="90,14,0,0" VerticalAlignment="Top" Width="120"/>
<Label Content="Current item" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="80"/>
</Grid>
</Window>
解决方案
new CollectionView() 导致 Visual Studio 2017 在调试输出中输出:
System.Windows.Data 警告:53:不完全支持直接使用 CollectionView。基本功能工作,虽然效率低下,但高级功能可能会遇到已知的错误。考虑使用派生类来避免这些问题。
相反,我将其更改为类型 ICollectionView,然后将 Items 设置为 CollectionViewSource.GetDefaultView(business.GetItemsFromWebService());
此外,您设置 wnd1.cbItems.Binding(ComboBox.SelectedItemProperty, xxx) 两次。我认为第二个应该是 wnd2。
如果绑定正确,则不需要 SelectionChanged 事件处理程序。
这是更新后的 MainWindow.cs 更改:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public ICollectionView Items { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public string SelectedItem
{
get { return (string)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem",
typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public MainWindow()
{
DataContext = this;
InitializeComponent();
Left = 100; Width = 350; Height = 200; Top = 10;
Business business = new Business();
Items = CollectionViewSource.GetDefaultView(business.GetItemsFromWebService());
Wnd1 wnd1 = new Wnd1();
wnd1.Left = 100; wnd1.Width = 350; wnd1.Height = 200; wnd1.Top = 210;
wnd1.Show();
Wnd2 wnd2 = new Wnd2();
wnd2.Left = 100; wnd2.Width = 350; wnd2.Height = 200; wnd2.Top = 410;
wnd2.Show();
wnd1.cbItems.ItemsSource = Items;
wnd2.cbItems.ItemsSource = Items;
Binding labelBinding = new Binding();
labelBinding.Mode = BindingMode.TwoWay;
labelBinding.Source = this;
labelBinding.Path = new PropertyPath("SelectedItem");
// no need to make an identical bindings, just use the same one again
lbSelected.SetBinding(Label.ContentProperty, labelBinding);
wnd1.cbItems.SetBinding(ComboBox.SelectedItemProperty, labelBinding);
wnd2.cbItems.SetBinding(ComboBox.SelectedItemProperty, labelBinding);
}
}
推荐阅读
- c# - DocFX - 无法从 API 文档中过滤掉公共方法
- javascript - 获取嵌套对象的所有唯一组合
- r - 查找给定短语附近的所有术语
- python - 使用字典来引用函数内的全局变量(Python)
- apache - 在 Nginx 反向代理后面的 Apache 中获取真正的访客 IP 不起作用
- arrays - 是否有 Python 替代 IDL 的 array_indices() 函数?
- python - 如何在python plotly scattergeo线图中分离不同的痕迹
- firebase - Firebase 存储始终可公开访问
- django-viewflow - 动态 NSubprocess - 启动额外的子进程 *在* NSuprocess 启动后
- html - 配置
使用样式</h1> <div id="body"><p>标签<code><xmp></code>有问题,我无法将其配置为自动缩放。我应该为那个 xmp 使用什么样式?我想根据页面宽度自动缩放。</p> <p><a href="https://i.stack.imgur.com/QqCcl.png" rel=" nofollow noreferrer"><img src="htt