依赖属性可以更新吗?何时更改依赖项属性所依赖的变量或字段? [英] Dependency Property can update? when variable or field on which Dependency Property Depends on being Changed?
问题描述
我需要做的是什么?
我需要制作一个一个表达式,其中依赖属性应该依赖于它?
假设如下
Count = dependOne + dependTwo;
在这里,Count
是依赖项属性,而dependOne
和dependTwo
是应该依赖于依赖项属性Count
的两个变量.
现在无论何时更改变量dependOne
或dependTwo
,依赖项属性都必须自动更新?
有可能吗?如果是,那怎么办?
看看下面的代码
XAML:
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 variable 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???
have a look at below code
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}" />
<Button Content="Button" Name="button1" Click="button1_Click" />
</Grid>
</Window>
代码提示:
CODE BEHIND:
using System;
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
}
}
}
推荐答案
Pritesh,您的问题的解决方案是使用PropertyChangedCallback并将EventHandler附加到它.
这是我的工作方式:
Hi Pritesh, the solution to your problem is by using PropertyChangedCallback and attach an EventHandler to it.
Here is how I did:
public partial class Window1 : Window
{
public static DependencyProperty CountProperty;
public static DependencyProperty DependOneProperty;
public static DependencyProperty DependTwoProperty;
static Window1()
{
CountProperty = DependencyProperty.Register("Count",
typeof(int),
typeof(Window1),
new UIPropertyMetadata(0));
// Use PropertyChangedCallBack, whenever DependOne property changes, the handler attached to it will be called.
DependOneProperty = DependencyProperty.Register("DependOne",
typeof(int),
typeof(Window1),
new UIPropertyMetadata(0,
new PropertyChangedCallback(OnDependChange)));
// Same goes for DependTwo but attaching to the same handler.
DependTwoProperty = DependencyProperty.Register("DependTwo",
typeof(int),
typeof(Window1),
new UIPropertyMetadata(0,
new PropertyChangedCallback(OnDependChange)));
}
public int Count
{
get { return (int)GetValue(CountProperty); }
set { SetValue(CountProperty, value); }
}
public int DependOne
{
get { return (int)GetValue(DependOneProperty); }
set { SetValue(DependOneProperty, value); }
}
public int DependTwo
{
get { return (int)GetValue(DependTwoProperty); }
set { SetValue(DependTwoProperty, value); }
}
private static void OnDependChange(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
Window1 window = (Window1)sender;
if (e.Property == DependOneProperty)
{
window.DependOne = (int)e.NewValue;
}
else
{
window.DependTwo = (int)e.NewValue;
}
window.Count = window.DependOne + window.DependTwo;
}
public Window1()
{
InitializeComponent();
this.DataContext = this;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
// When you "set" the DependOne and DependTwo, OnDependChange will be called and the Count will be updated.
DependOne = DependOne + 1;
DependTwo = DependTwo + 1;
}
}
希望这可以帮助. :)
如果您有任何疑问,请告诉我.
Hope this helps. :)
Do let me know if you have any doubts.
这篇关于依赖属性可以更新吗?何时更改依赖项属性所依赖的变量或字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!