当依赖属性所依赖的变量或字段发生变化时更新依赖属性
我需要做的是?
我需要创建一个表达式,依赖属性应该依赖于该表达式。
假设如下:
Count = dependOne + dependTwo;
这里,Count
是依赖属性,dependOne
和dependTwo
是依赖属性所依赖的两个变量计数
应该取决于。
现在,每当我更改变量 dependOne
或 dependTwo
时,依赖项属性都应该自动更新。
是否可以?如果是的话怎么办?
请参阅下面的代码
XAML:
<Window x:Class="DependecnyProperty.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Background="LightGray" Text="{Binding Path=Count}" Height="23" HorizontalAlignment="Left" Margin="164,102,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="164,148,0,0" Name="button1" VerticalAlignment="Top" Width="120" Click="button1_Click" />
</Grid>
</Window>
代码隐藏:
namespace DependecnyProperty
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Count = dependOne + dependTwo;
}
int dependOne = 0;
int dependTwo = 0;
public int Count
{
get { return (int)GetValue(CountProperty); }
set { SetValue(CountProperty, value); }
}
// Using a DependencyProperty as the backing store for Count. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CountProperty =
DependencyProperty.Register("Count", typeof(int), typeof(MainWindow), new UIPropertyMetadata(12));
private void button1_Click(object sender, RoutedEventArgs e)
{
dependOne = dependOne + 2;
dependTwo = dependTwo + 1;
//I need to find way ...now here i have changed value of two variable.
//now it is possible to change Dependency Property
//Without here setting the value of dependency property
}
}
}
编辑更新的代码隐藏:
namespace DependecnyProperty
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ViewModel vm;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
vm = new ViewModel();
//this.DataContext = vm;
Count = vm.DependOne + vm.DependTwo;
}
public int Count
{
get { return (int)GetValue(CountProperty); }
set { SetValue(CountProperty, value); }
}
// Using a DependencyProperty as the backing store for Count. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CountProperty =
DependencyProperty.Register("Count", typeof(int), typeof(MainWindow), new UIPropertyMetadata(12));
private void button1_Click(object sender, RoutedEventArgs e)
{
vm.DependOne++;
vm.DependTwo++;
//I need to find way ...now here i have changed value of two variable.
//now it is possible to change Dependency Property
//Without here setting the value of dependency property
}
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
}
private int dependOne;
public int DependOne
{
get
{
return dependOne;
}
set
{
dependOne = value;
OnPropertyChanged("DependOne");
}
}
private int dependTwo;
public int DependTwo
{
get
{
return dependTwo;
}
set
{
dependTwo = value;
OnPropertyChanged("DependTwo");
}
}
#region -- INotifyPropertyChanged Members --
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyNameArg)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyNameArg));
}
}
#endregion
}
}
}
What I need to do is?
I need to make one expression on which dependency property should depends on.
Suppose as per below:
Count = dependOne + dependTwo;
Here, Count
is Dependency property and dependOne
and dependTwo
are two variables on which dependency property Count
should Depends on.
Now whenever I change variable dependOne
or dependTwo
the dependency property should have to update automatically.
Is it possible? If yes then how?
See the code below
XAML:
<Window x:Class="DependecnyProperty.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Background="LightGray" Text="{Binding Path=Count}" Height="23" HorizontalAlignment="Left" Margin="164,102,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="164,148,0,0" Name="button1" VerticalAlignment="Top" Width="120" Click="button1_Click" />
</Grid>
</Window>
CODE BEHIND:
namespace DependecnyProperty
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Count = dependOne + dependTwo;
}
int dependOne = 0;
int dependTwo = 0;
public int Count
{
get { return (int)GetValue(CountProperty); }
set { SetValue(CountProperty, value); }
}
// Using a DependencyProperty as the backing store for Count. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CountProperty =
DependencyProperty.Register("Count", typeof(int), typeof(MainWindow), new UIPropertyMetadata(12));
private void button1_Click(object sender, RoutedEventArgs e)
{
dependOne = dependOne + 2;
dependTwo = dependTwo + 1;
//I need to find way ...now here i have changed value of two variable.
//now it is possible to change Dependency Property
//Without here setting the value of dependency property
}
}
}
EDIT UPDATED CODEBEHIND:
namespace DependecnyProperty
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ViewModel vm;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
vm = new ViewModel();
//this.DataContext = vm;
Count = vm.DependOne + vm.DependTwo;
}
public int Count
{
get { return (int)GetValue(CountProperty); }
set { SetValue(CountProperty, value); }
}
// Using a DependencyProperty as the backing store for Count. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CountProperty =
DependencyProperty.Register("Count", typeof(int), typeof(MainWindow), new UIPropertyMetadata(12));
private void button1_Click(object sender, RoutedEventArgs e)
{
vm.DependOne++;
vm.DependTwo++;
//I need to find way ...now here i have changed value of two variable.
//now it is possible to change Dependency Property
//Without here setting the value of dependency property
}
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
}
private int dependOne;
public int DependOne
{
get
{
return dependOne;
}
set
{
dependOne = value;
OnPropertyChanged("DependOne");
}
}
private int dependTwo;
public int DependTwo
{
get
{
return dependTwo;
}
set
{
dependTwo = value;
OnPropertyChanged("DependTwo");
}
}
#region -- INotifyPropertyChanged Members --
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyNameArg)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyNameArg));
}
}
#endregion
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道什么是最适合您的解决方案。然而,其中之一是您使用以下属性:
更新:
重要的是您不要在代码隐藏中更新 ViewModel 属性。相反,您可以调用 ViewModel 方法。
XAML
代码
I don't know what is the best solution for you. One of these ,however, would be that you use Property like:
UPDATED:
The important thing is that you don't update ViewModel properties in Codebehind. Instead, you could call a ViewModel method.
XAML
CODE
我会:
dependOne
和dependTwo
放入视图模型中INotifyPropertyChanged
添加到视图模型更新时,setter 就会自动被调用。
此解决方案的另一个优点是您可以将计数器逻辑与依赖属性逻辑分开。单元测试会更容易,并且可能更容易在不同场景中重用。
I'd:
dependOne
anddependTwo
in a View ModelINotifyPropertyChanged
to the View ModelThen the setter would automatically get called whenever those properties were updated.
Another advantage of this solution is that you could separate the counter logic from the dependency property logic. It would be easier to unit test, and possibly easier to reuse in different scenarios.
与 Merlyn Morgan-Graham 的答案非常相似的方法是,您可以在 ViewModel 中引入另一个名为 Count 的只读属性。每当 DependOne 或 DependTwo 更改时引发 PropertyChanged("Count") ,然后您可以针对 Count 进行 OneWay 绑定
然后绑定就像这样简单
A pretty similar approach to the answer by Merlyn Morgan-Graham, you can introduce another readonly property called Count in your ViewModel. Raise PropertyChanged("Count") whenever DependOne or DependTwo changes, then you can do a OneWay binding against Count
Then binding is just as simple as