2011-07-16 118 views
1

我需要做的是?依赖于变量的变量或字段时依赖项属性的更新

我需要做一个表达式上依赖属性应该依赖。

假设按如下:

Count = dependOne + dependTwo; 

这里,Count依赖属性dependOnedependTwo是上依赖属性Count应该取决于两个变量。

现在无论何时我更改变量dependOnedependTwo,依赖项属性都应该自动更新。

可能吗?如果是,那么如何?

参见下面

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 
     } 
    } 
} 

回答

3

我不知道什么是最适合您的解决方案。其中的一个,但是,这是你使用类似物业:

//field 
    private int _dependOne; 

    //property 
    public int DependOne 
    { 
     get { return _dependOne; } 
     set { 
      _dependOne = value; 
      Count += value; 
     } 
    } 

    //Finally, use the property instead of the field 
    //dependOne = dependOne + 2; 
    DependOne += 2; 

更新: 重要的是,你不更新代码隐藏视图模型属性。相反,你可以调用ViewModel方法。

XAML

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Local="clr-namespace:WpfApplication1" 
     Title="MainWindow" Height="350" Width="525"> 

    <StackPanel> 
     <StackPanel.Resources> 
      <Local:MyConverter x:Key="myConverter"/> 
     </StackPanel.Resources> 
     <TextBox> 
      <TextBox.Text> 
       <MultiBinding Converter="{StaticResource myConverter}"> 
        <Binding Path="DependOne" Mode="OneWay"/> 
        <Binding Path="DependTwo" Mode="OneWay"/> 
       </MultiBinding> 
      </TextBox.Text> 
     </TextBox> 
     <Button Click="button1_Click">click</Button> 
    </StackPanel> 
</Window> 

CODE

public partial class MainWindow : Window 
{ 
    ViewModel vm; 
    public MainWindow() 
    { 
     InitializeComponent(); 

     vm = new ViewModel(); 
     this.DataContext = vm; 
    } 


    //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.UpdateCount(); 
    } 


} 

public class ViewModel : INotifyPropertyChanged 
{ 

    public ViewModel() 
    { 

    } 

    private int dependOne = 0; 

    public int DependOne 
    { 
     get 
     { 
      return dependOne; 
     } 
     set 
     { 
      dependOne = value; 
      OnPropertyChanged("DependOne"); 
     } 
    } 

    private int dependTwo = 0; 

    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 

    internal void UpdateCount() 
    { 
     DependOne = 3; 
     DependTwo = 4; 
    } 
} 

public class MyConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     var dependOne = (int)values[0]; 
     var dependTwo = (int)values[1]; 

     return (dependOne + dependTwo).ToString(); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 
+0

雅这其中也使得SENCE .. ... –

+0

是的,这是一个简单的...因为不知道你的实际情况,我只是这么说,但我认为@Merlyn Morgan-Graham是一个好点。另外,如果dependOne作用于View,则可以考虑将dependOne作为“DependecnyProperty”。然后,使用“Value Changed Callback”更新计数。 –

+0

谢谢你回答..但..,根据格雷厄姆的答案.....我们必须把表达式...'计数= dependOne + dependTwo; ' –

3

我倒是:

  • dependOnedependTwo在视图模型
  • 添加INotifyPropertyChanged向视图模型
  • 提高时,这些属性的制定者接到电话
  • 使用MultiBinding的依赖属性

然后二传手绑定到这两个视图模型的属性会自动将属性更改事件每当这些属性被更新时都会被调用。

此解决方案的另一个优点是可以将计数器逻辑与依赖项属性逻辑分开。单元测试会更容易,并且可能更容易在不同情况下重用。

+0

好吧..我正在尝试....感谢回答.... –

+0

@格拉姆姆,,,我很困惑把'表达式'..... .....'计数= dependOne + dependTwo;' –

+0

@Aryan:你可以把它放在一个自定义的'IMultiValueConverter'中,与'MultiBinding'一起使用。 –

2

一个非常相似的方法来回答Merlyn Morgan-Graham,您可以在ViewModel中引入另一个名为Count的只读属性。提高的PropertyChanged(“计数”),每当DependOne或DependTwo变化,那么你可以做一个单向对伯爵结合

private int m_dependOne; 
public int DependOne 
{ 
    get { return m_dependOne; } 
    set 
    { 
     m_dependOne = value; 
     OnPropertyChanged("DependOne"); 
     OnPropertyChanged("Count"); 
    } 
} 

private int m_dependTwo; 
public int DependTwo 
{ 
    get { return m_dependTwo; } 
    set 
    { 
     m_dependTwo = value; 
     OnPropertyChanged("DependTwo"); 
     OnPropertyChanged("Count"); 
    } 
} 

public int Count 
{ 
    get 
    { 
     return m_dependOne + m_dependTwo; 
    } 
} 

然后绑定是一样简单

<TextBlock Text="{Binding Count, Mode=OneWay}"/>