【发布时间】:2018-01-27 21:43:11
【问题描述】:
是否可以将一个属性绑定到多个控件?
例如,我想制作两个控件,它们可以在点击时增加一个值,并且都可以访问这个总和。
<Grid>
<local:CustomControl Name="Control1" CommonValue="0"/>
<local:CustomControl Name="Control2" CommonValue="0"/>
<TextBlock Name="Counter" Text="{<binding to Control1.CommonValue and Control2.CommonValue>}"/>
</Grid>
public partial class CustomControl : UserControl, INotifyPropertyChanged
{
...
private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
{
CommonValue = (int.Parse(CommonValue) + 1).ToString();
}
private string commonValue= "0";
public event PropertyChangedEventHandler PropertyChanged;
public string CommonValue
{
get { return commonValue; }
set
{
commonValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CommonValue"));
}
}
【问题讨论】:
-
您想显示 control1.CommonValue 和 control2.CommonValue 的总和并在文本块中显示结果,对吗?如果是这样,您可以使用转换器进行多重绑定,将 2 个数字相加并返回结果
-
@Milan 不,我想从
CustomControls 获得总和