C#WPF使用位于另一个项目中的dependency属性更新两个用户控件
我试图在WPF中为两个不同的用户控件设置一个公共依赖属性。 我尝试了很多我发现的解决方案,但没有一个奏效C#WPF使用位于另一个项目中的dependency属性更新两个用户控件,c#,wpf,dependency-properties,C#,Wpf,Dependency Properties,我试图在WPF中为两个不同的用户控件设置一个公共依赖属性。 我尝试了很多我发现的解决方案,但没有一个奏效 因此,目前我得到的信息如下: 我有一个包含公共属性的类,它当前(在尝试了几乎所有东西之后)看起来是这样的: namespace CommonProperties { public class CommonProp : DependencyObject, INotifyPropertyChanged { public static readonly DependencyProperty
因此,目前我得到的信息如下: 我有一个包含公共属性的类,它当前(在尝试了几乎所有东西之后)看起来是这样的:
namespace CommonProperties {
public class CommonProp : DependencyObject, INotifyPropertyChanged
{
public static readonly DependencyProperty IsTrueProperty =
DependencyProperty.Register("IsTrue", typeof(bool), typeof(CommonProp), new PropertyMetadata(false));
private bool _isTrue;
public bool IsTrue
{
get { return (bool)GetValue(IsTrueProperty); }
set { SetValue(IsTrueProperty, value); NotifyPropertyChanged("IsTrue"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string nomPropriete)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(nomPropriete));
}
}}
我还有两个类似的用户控件:UC1:
<UserControl x:Class="ClassLibUC1.UC1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:CommonProperties;assembly=CommonProperties"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<TextBox x:Name="text1"
Width="254"
Height="23"
Margin="24,24,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="{Binding IsTrue}"
TextWrapping="Wrap" />
<Button Width="75"
Margin="70,68,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="Button_Click"
Content="Button" />
</Grid>
第二个用户控件完全相同。问题是,当我单击第一个或第二个用户控件中的按钮时,它只会更新所选控件,而不会同时更新这两个控件。。知道如何为两个控件实现一个公共属性吗?使用Dependency属性我们无法实现这一点,因为Dependency属性属于一个实例,这意味着如果您有两个用户控件实例,更新一个控件的DependencyProperty值将永远不会更新另一个控件的DependencyProperty,因为它是一个不同的实例 但我们仍然可以实现为所有同一类型的用户控件(如您的)更新相同值的要求。为此,我们必须在xaml.cs文件中做一些修改,如下所示
/// <summary>
/// Interaction logic for UC1.xaml
/// </summary>
public partial class UC1 : UserControl
{
private static List<UC1> _allInstanceOfThisControl = new List<UC1>();
ViewModelUC1 vm = new ViewModelUC1();
public UC1()
{
InitializeComponent();
this.DataContext = vm;
_allInstanceOfThisControl.Add(this);
this.Unloaded += UC1_Unloaded;
}
private void UC1_Unloaded(object sender, RoutedEventArgs e)
{
_allInstanceOfThisControl.Remove(this);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (vm.IsTrue)
{
vm.IsTrue = false;
}
else
{
vm.IsTrue = true;
}
var _allInstanceOfThisControlExceptthis = _allInstanceOfThisControl.Where(s => s != this).ToList();
_allInstanceOfThisControlExceptthis.ForEach(s =>
{
(s.DataContext as ViewModelUC1).IsTrue = vm.IsTrue;
});
}
//
///UC1.xaml的交互逻辑
///
公共部分类UC1:UserControl
{
私有静态列表_allInstanceOfThisControl=新列表();
ViewModelUC1 vm=新的ViewModelUC1();
公共UC1()
{
初始化组件();
this.DataContext=vm;
_ALINSTANCOFTHISCONTROL.Add(此);
this.unload+=UC1\u unload;
}
私有无效UC1_已卸载(对象发送方,路由目标e)
{
_所有控制装置。移除(此);
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
如果(虚拟现实)
{
vm.IsTrue=false;
}
其他的
{
vm.IsTrue=true;
}
var_allInstanceOfThisControlExceptthis=_allInstanceOfThisControl.Where(s=>s!=this.ToList();
_AllInstanceOfIsControlExceptThis.ForEach(s=>
{
(s.DataContext作为ViewModelUC1.IsTrue=vm.IsTrue;
});
}
希望这将有助于实现您的要求。您可以使用静态正常属性(或字段)为所有实例保存相同的值。另一个选项是将值存储在外部。如果有外部模型/视图模型来保存
bool
值,则用户控件的所有实例都可以使用它(在视图中或其视图模型)。
public partial class UC1 : UserControl
{
ViewModelUC1 vm = new ViewModelUC1();
public UC1()
{
InitializeComponent();
this.DataContext = vm;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CommonProp prop = new CommonProp();
if (vm.IsTrue)
{
vm.IsTrue = false;
}
else
{
vm.IsTrue = true;
}
}
}
/// <summary>
/// Interaction logic for UC1.xaml
/// </summary>
public partial class UC1 : UserControl
{
private static List<UC1> _allInstanceOfThisControl = new List<UC1>();
ViewModelUC1 vm = new ViewModelUC1();
public UC1()
{
InitializeComponent();
this.DataContext = vm;
_allInstanceOfThisControl.Add(this);
this.Unloaded += UC1_Unloaded;
}
private void UC1_Unloaded(object sender, RoutedEventArgs e)
{
_allInstanceOfThisControl.Remove(this);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (vm.IsTrue)
{
vm.IsTrue = false;
}
else
{
vm.IsTrue = true;
}
var _allInstanceOfThisControlExceptthis = _allInstanceOfThisControl.Where(s => s != this).ToList();
_allInstanceOfThisControlExceptthis.ForEach(s =>
{
(s.DataContext as ViewModelUC1).IsTrue = vm.IsTrue;
});
}