代码之家  ›  专栏  ›  技术社区  ›  Begtostudy

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

  •  4
  • Begtostudy  · 技术社区  · 15 年前

    我想用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();
            }
        }
    

    <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>
    
    4 回复  |  直到 15 年前
        1
  •  5
  •   Nir    15 年前

    缺少对PropertyChangedEventArgs构造函数调用中文本的引号:

    Text = "Begin";
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs("Text"));
    
        2
  •  3
  •   HCL    15 年前

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

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

    它会完成任务的。

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

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

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

    然后从你的财产设定者那里打电话给他们。

        3
  •  2
  •   Matas Vaitkevicius user3782709    11 年前

    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>
    
        4
  •  2
  •   Faenrig    7 年前

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

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

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