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

问题
答案
-
你好,
以上面这段代码为例,在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月8日 0:17
全部回复
-
你好,
我在前台写了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.
- 已编辑 Annie Chen-MSFTModerator 2016年12月7日 5:30
- 已标记为答案 轮回的齿轮 2016年12月7日 7:53
- 取消答案标记 轮回的齿轮 2016年12月7日 7:53
-
你好,
我在前台写了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日 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月8日 0:17