WPF 字符串属性与 Button.Content 的绑定

发布于 2024-09-10 22:36:00 字数 2489 浏览 6 评论 0原文

我想将字符串属性与 Button.Content 绑定。

但为什么没有效果呢?

数据类:

namespace test4
{
    public class Test : INotifyPropertyChanged
    {

         string   _Text = "Begin";

        public string Text
        {
            get{return _Text;}
            protected set { _Text = value; }
        }
        public void Start()
        {
            Text = "Begin";
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Text));
        }
        public void End()
        {
            Text = "End";
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Text));
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}

逻辑代码:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        test4.Test ttt = new test4.Test();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource testViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("testViewSource")));
            testViewSource.Source = new object[]{ttt};
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ttt.Text == "Begin")
                ttt.End();
            else
                ttt.Start();
        }
    }

XAML:

<Window x:Class="test5.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" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:test4" Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="testViewSource" d:DesignSource="{d:DesignInstance my:Test, CreateList=true}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource testViewSource}">
        <Button Content="{Binding Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="158,95,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

I want to bind string property with Button.Content.

But why it didn't work?

The data class:

namespace test4
{
    public class Test : INotifyPropertyChanged
    {

         string   _Text = "Begin";

        public string Text
        {
            get{return _Text;}
            protected set { _Text = value; }
        }
        public void Start()
        {
            Text = "Begin";
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Text));
        }
        public void End()
        {
            Text = "End";
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Text));
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}

The logical code:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        test4.Test ttt = new test4.Test();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource testViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("testViewSource")));
            testViewSource.Source = new object[]{ttt};
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ttt.Text == "Begin")
                ttt.End();
            else
                ttt.Start();
        }
    }

The XAML:

<Window x:Class="test5.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" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:test4" Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="testViewSource" d:DesignSource="{d:DesignInstance my:Test, CreateList=true}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource testViewSource}">
        <Button Content="{Binding Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="158,95,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失与倦" 2024-09-17 22:36:00

对 PropertyChangedEventArgs 构造函数的调用中 Text 周围的引号丢失:

Text = "Begin";
if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs("Text"));

The quotes around Text in the call to the PropertyChangedEventArgs constructor are missing:

Text = "Begin";
if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs("Text"));
尛丟丟 2024-09-17 22:36:00

PropertyChanged 想要查看已更改的属性的名称而不是值。将事件调用更改为:

PropertyChanged(this, new PropertyChangedEventArgs("Text")); 

它将完成工作。
不过,我也会将构造更改为

public string Text { 
     get{return _Text;} 
     protected set { 
          _Text = value; 
          if(null != PropertyChanged){
                 PropertyChanged(this,new PropertyChangedEventArgs("Text"));
          }   
     } 
} 

,然后不要从开始和结束调用 PropertyChanged 事件。

更进一步,创建如下调用方法:

protected virtual void OnPropertyChanged(string propertyName) {
    OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
    if (null != PropertyChanged) {
        PropertyChanged(this,e);
    }
}

然后从属性设置器中调用它们。

PropertyChanged wants to see the name of the property that has changed and not the value. Change the event invocation to:

PropertyChanged(this, new PropertyChangedEventArgs("Text")); 

and it will do the job.
However I also would change the construction to

public string Text { 
     get{return _Text;} 
     protected set { 
          _Text = value; 
          if(null != PropertyChanged){
                 PropertyChanged(this,new PropertyChangedEventArgs("Text"));
          }   
     } 
} 

and then dont call the PropertyChanged-event from Start and End.

And to go even a step further, create invocation-methods like:

protected virtual void OnPropertyChanged(string propertyName) {
    OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
    if (null != PropertyChanged) {
        PropertyChanged(this,e);
    }
}

and then call them from your property-setter.

心舞飞扬 2024-09-17 22:36:00

首先,我使用了objectdataproviding,但它不是对象“ttt”,它们是两个不同的东西。

其次,PropertyChanged(this,new PropertyChangedEventArgs("Text"));“Text”是名称而不是变量。

因此,代码如下,可能对其他人有帮助。

数据类:

namespace test3
{
    public class Test : INotifyPropertyChanged
    {

         string   _Text = "Begin";

        public string Text
        {
            get{return _Text;}
            protected set { _Text = value;
            NotifyPropertyChanged("Text");
            }
        }
        public void Start()
        {
            Text = "Begin";

        }
        public void End()
        {
            Text = "End";

        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        } 
    }
}

逻辑 cs:

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

            button1.DataContext=ttt;
        } 

        Test ttt = new Test();

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ttt.Text == "Begin")
                ttt.End();
            else
                ttt.Start();
        }
    }
}

xaml:

<Window x:Class="test3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:test3"
        Title="MainWindow" Height="350" Width="525" >
    <Grid>
        <Button Content="{Binding Path=Text,UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="121,69,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

First of all, I used objectdataproviding, but it is not the object "ttt", they are two different things.

Second, PropertyChanged(this,new PropertyChangedEventArgs("Text")); "Text" is the name not the variable.

So, the code is following, may be helpful to others.

The data class:

namespace test3
{
    public class Test : INotifyPropertyChanged
    {

         string   _Text = "Begin";

        public string Text
        {
            get{return _Text;}
            protected set { _Text = value;
            NotifyPropertyChanged("Text");
            }
        }
        public void Start()
        {
            Text = "Begin";

        }
        public void End()
        {
            Text = "End";

        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        } 
    }
}

The logical cs:

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

            button1.DataContext=ttt;
        } 

        Test ttt = new Test();

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ttt.Text == "Begin")
                ttt.End();
            else
                ttt.Start();
        }
    }
}

The xaml:

<Window x:Class="test3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:test3"
        Title="MainWindow" Height="350" Width="525" >
    <Grid>
        <Button Content="{Binding Path=Text,UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="121,69,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>
丶视觉 2024-09-17 22:36:00

您需要在属性周围使用引号:

public void Start()
{
    Text = "Begin";
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}

我建议使用 nameof() 方法,因为当属性位于引号之间时,您无法重命名该属性:

public void Start()
{
    Text = "Begin";
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(nameof(Text)));
}

You need to use quotes around the property:

public void Start()
{
    Text = "Begin";
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}

I suggest to rather use the nameof()-method as you cannot rename the property while it's located between quotes:

public void Start()
{
    Text = "Begin";
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(nameof(Text)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文