Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当依赖的变量或字段发生更改时,更新依赖属性_C#_.net_Wpf_Dependency Properties - Fatal编程技术网

C# 当依赖的变量或字段发生更改时,更新依赖属性

C# 当依赖的变量或字段发生更改时,更新依赖属性,c#,.net,wpf,dependency-properties,C#,.net,Wpf,Dependency Properties,我需要做的是什么 我需要做一个表达式,依赖属性应该依赖于这个表达式 假设如下: Count = dependOne + dependTwo; 这里,Count是依赖性属性和dependOne和dependTwo是依赖性属性Count应该依赖的两个变量 现在,每当我更改变量dependOne或dependtow2时,依赖项属性都必须自动更新 可能吗?如果是,那怎么办 请参见下面的代码 XAML: <Window x:Class="DependecnyProperty.MainWindow"

我需要做的是什么

我需要做一个表达式,依赖属性应该依赖于这个表达式

假设如下:

Count = dependOne + dependTwo;
这里,
Count
依赖性属性
dependOne
dependTwo
是依赖性属性
Count
应该依赖的两个变量

现在,每当我更改变量
dependOne
dependtow2
时,依赖项属性都必须自动更新

可能吗?如果是,那怎么办

请参见下面的代码

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

代码隐藏:

<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
        }
    }
}
namespace DependecnyProperty
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=this;
计数=dependOne+dependTwo;
}
int dependOne=0;
int dependtow2=0;
公共整数计数
{
获取{return(int)GetValue(CountProperty);}
set{SetValue(CountProperty,value);}
}
//使用DependencyProperty作为计数的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty CountProperty=
DependencyProperty.Register(“计数”、typeof(int)、typeof(MainWindow)、新UIPropertyMetadata(12));
私有无效按钮1\u单击(对象发送者,路由目标)
{
dependOne=dependOne+2;
dependTwo=dependTwo+1;
//我需要找到方法…现在我已经改变了两个变量的值。
//现在可以更改依赖项属性了
//在这里不设置dependency属性的值
}
}
}
编辑更新的代码隐藏:

<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
        }
    }
}
namespace DependecnyProperty
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
视图模型虚拟机;
公共主窗口()
{
初始化组件();
this.DataContext=this;
vm=新的ViewModel();
//this.DataContext=vm;
Count=vm.DependOne+vm.dependtow2;
}
公共整数计数
{
获取{return(int)GetValue(CountProperty);}
set{SetValue(CountProperty,value);}
}
//使用DependencyProperty作为计数的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty CountProperty=
DependencyProperty.Register(“计数”、typeof(int)、typeof(MainWindow)、新UIPropertyMetadata(12));
私有无效按钮1\u单击(对象发送者,路由目标)
{
vm.DependOne++;
vm.dependtow2++;
//我需要找到方法…现在我已经改变了两个变量的值。
//现在可以更改依赖项属性了
//在这里不设置dependency属性的值
}
公共类视图模型:INotifyPropertyChanged
{
公共视图模型()
{
}
独立的私人公司;
公共int DependOne
{
得到
{
返回dependOne;
}
设置
{ 
dependOne=值;
不动产变更(“依赖”);
}
}
私有int依赖于两个;
公共int依赖于2
{
得到
{ 
返回依赖项2;
}
设置
{ 
dependTwo=值;
OnPropertyChanged(“DependTwo”);
}
}
#区域--INotifyPropertyChanged成员--
公共事件属性更改事件处理程序属性更改;
OnPropertyChanged上的公共无效(字符串propertyNameArg)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyNameArg));
}
}
#端区
}
}
}

我不知道什么是最适合您的解决方案。但是,其中之一是使用如下属性:

    //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;

更新: 重要的是不要在Codebehind中更新ViewModel属性。相反,您可以调用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>

我不知道什么是对你最好的解决方案。但是,其中之一是使用如下属性:

    //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;

更新: 重要的是不要在Codebehind中更新ViewModel属性。相反,您可以调用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>
我想:

  • 在视图模型中放置
    dependOne
    dependtow2
  • 将INotifyPropertyChanged添加到视图模型
  • 调用属性的setter时引发属性更改事件
  • 使用将依赖项属性绑定到这两个视图模型属性
然后,每当更新这些属性时,setter就会自动被调用

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

我想:

  • 在视图模型中放置
    dependOne
    dependtow2
  • 将INotifyPropertyChanged添加到视图模型
  • 筹集财产