none
如何wpf运行过程中更改绑定的后台对象?或者怎么使数据模板中的两个textblock合并到一起? RRS feed

  • 问题

  • 绑定泛型集合的实体对象类型有两个string属性a和b。实现这样一个功能,用户做出行为触发事件,该事件将一个textblock控件(用户看来就是一个textblock)显示b/a切换成显示a/b。就是textblock根据需求显示实体对象的两个不同的字符串属性。

    一种思路程序是更改textblock绑定的后台对象,怎么做?

    另一种思路是在数据模板中把两个textblock放到一起,一个隐藏另一个必须显示,主要是放在一起怎么做?

    可以的话想要第一种方式


    2016年12月6日 6:00

答案

  • 你好,
    以上面这段代码为例,在button点击事件中更改绑定的Path就可以了:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Binding binding = new Binding();
                binding.Path = new PropertyPath("sCity");
                binding.Source = ss;
    
                BindingOperations.SetBinding(textblock1, TextBlock.TextProperty, binding);
            }

    Best Regards,

    Annievia Chen


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年12月7日 9:24
    版主

全部回复

  • 你好,
    我在前台写了textblock和一个button。在后台设置绑定,点击button交换后台两个string的内容,再次点击后再次交换。实际应用时根据要实现的功能再修改相关部分就可以。

    XAML:

        <StackPanel x:Name="stackPanel1">
            <TextBlock x:Name="textblock1" Width="100"  Height="30" Background="AliceBlue" HorizontalAlignment="Left" />
            <Button Width="100" Height="30" Content="Exchange" Click="Button_Click"/>
        </StackPanel>

    C#:

        public partial class MainWindow : Window
    
        {
            public class Student:INotifyPropertyChanged
            {
                private string _sName;
                public string _sCity;
                public string sName
                {
                    get { return _sName; }
                    set
                    {
                        _sName = value;
                        sNamePropertyChanged("sName");
                    }
                }
                public string sCity
                {
                    get { return _sCity; }
                    set
                    {
                        _sCity = value;
                        sCityPropertyChanged("sCity");
                    }
                }
                public event PropertyChangedEventHandler PropertyChanged;
                protected void sNamePropertyChanged(string sName)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(sName));
                }
                protected void sCityPropertyChanged(string sCity)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(sCity));
                }
    
            }
    
            Student ss = new Student() { sName = "Rita", sCity = "NewYork"};
            public MainWindow()
            {
                InitializeComponent();
                Binding binding = new Binding();
                binding.Path = new PropertyPath("sName");
                binding.Source = ss;
    
                BindingOperations.SetBinding(textblock1, TextBlock.TextProperty, binding);
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                String temp;
                temp = ss.sName;
                ss.sName = ss.sCity;
                ss.sCity = temp;
            }
    
        }

    Best Regards,

    Annievia Chen


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    2016年12月7日 5:11
    版主
  • 你好,
    我在前台写了textblock和一个button。在后台设置绑定,点击button交换后台两个string的内容,再次点击后再次交换。实际应用时根据要实现的功能再修改相关部分就可以。

    XAML:

        <StackPanel x:Name="stackPanel1">
            <TextBlock x:Name="textblock1" Width="100"  Height="30" Background="AliceBlue" HorizontalAlignment="Left" />
            <Button Width="100" Height="30" Content="Exchange" Click="Button_Click"/>
        </StackPanel>

    C#:

        public partial class MainWindow : Window
    
        {
            public class Student:INotifyPropertyChanged
            {
                private string _sName;
                public string _sCity;
                public string sName
                {
                    get { return _sName; }
                    set
                    {
                        _sName = value;
                        sNamePropertyChanged("sName");
                    }
                }
                public string sCity
                {
                    get { return _sCity; }
                    set
                    {
                        _sCity = value;
                        sCityPropertyChanged("sCity");
                    }
                }
                public event PropertyChangedEventHandler PropertyChanged;
                protected void sNamePropertyChanged(string sName)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(sName));
                }
                protected void sCityPropertyChanged(string sCity)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(sCity));
                }
    
            }
    
            Student ss = new Student() { sName = "Rita", sCity = "NewYork"};
            public MainWindow()
            {
                InitializeComponent();
                Binding binding = new Binding();
                binding.Path = new PropertyPath("sName");
                binding.Source = ss;
    
                BindingOperations.SetBinding(textblock1, TextBlock.TextProperty, binding);
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                String temp;
                temp = ss.sName;
                ss.sName = ss.sCity;
                ss.sCity = temp;
            }
    
        }

    Best Regards,

    Annievia Chen


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    我看了一下,好像是对于单个textblock绑定对象切换。我希望是对数据模板中的textblock绑定的后台数据对象切换
    2016年12月7日 7:55
  • 你好,
    以上面这段代码为例,在button点击事件中更改绑定的Path就可以了:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Binding binding = new Binding();
                binding.Path = new PropertyPath("sCity");
                binding.Source = ss;
    
                BindingOperations.SetBinding(textblock1, TextBlock.TextProperty, binding);
            }

    Best Regards,

    Annievia Chen


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年12月7日 9:24
    版主