我需要做的是?依賴於變量的變量或字段時依賴項屬性的更新
我需要做一個表達式上依賴屬性應該依賴。
假設按如下:
Count = dependOne + dependTwo;
這裏,Count
是依賴屬性和dependOne
和dependTwo
是上依賴屬性Count
應該取決於兩個變量。
現在無論何時我更改變量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
}
}
}
雅這其中也使得SENCE .. ... –
是的,這是一個簡單的...因爲不知道你的實際情況,我只是這麼說,但我認爲@Merlyn Morgan-Graham是一個好點。另外,如果dependOne作用於View,則可以考慮將dependOne作爲「DependecnyProperty」。然後,使用「Value Changed Callback」更新計數。 –
謝謝你回答..但..,根據格雷厄姆的答案.....我們必須把表達式...'計數= dependOne + dependTwo; ' –