C# WPF以编程方式双向绑定
我试图绑定一对切换按钮,所以当一个被选中时,另一个也被选中C# WPF以编程方式双向绑定,c#,wpf,binding,C#,Wpf,Binding,我试图绑定一对切换按钮,所以当一个被选中时,另一个也被选中 ToggleButton two = new ToggleButton(); /* Set up ToggleButton here*/ ToggleButton one = this.somePanel.Children.FirstOrDefault(/*Some Condition*/) as ToggleButton if (one == null) return; Binding binding = new Binding("I
ToggleButton two = new ToggleButton();
/* Set up ToggleButton here*/
ToggleButton one = this.somePanel.Children.FirstOrDefault(/*Some Condition*/) as ToggleButton
if (one == null) return;
Binding binding = new Binding("IsChecked");
binding.Source = two;
binding.Mode = BindingMode.TwoWay;
one.SetBinding(ToggleButton.IsCheckedProperty, binding);
/* Add two to the UI */
当我切换按钮1时,按钮2会切换,但是,当我切换按钮2时,按钮1不会切换。我认为您的代码没有任何问题。在我这边,它似乎工作得很好。我已经在xaml和codebehind中进行了尝试,也完全在codebehind中进行了尝试 这两个作品的预期和绑定是完美的 我在下面附上了一个样本。检查一下。我这边的装订很好 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:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
>
<Grid x:Name="Grid1" />
</Window>
代码隐藏:
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ToggleButton two = new ToggleButton();
two.Content = "Two";
two.Width = 100;
two.Height = 50;
/* Set up ToggleButton here*/
this.Grid1.Children.Add(two);
ToggleButton one = new ToggleButton();
if (one == null) return;
one.Content = "One";
one.Width = 100;
one.Height = 50;
one.Margin = new Thickness(0, 0, 250, 0);
this.Grid1.Children.Add(one);
Binding binding = new Binding("IsChecked");
binding.Source = two;
binding.Mode = BindingMode.TwoWay;
one.SetBinding(ToggleButton.IsCheckedProperty, binding);
/* Add two to the UI */
}
}
}
使用System.Windows;
使用System.Windows.Controls.Primitives;
使用System.Windows.Data;
命名空间WpfApplication1
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
ToggleButton二=新的ToggleButton();
二、Content=“二”;
2.宽度=100;
2.高度=50;
/*在这里设置切换按钮*/
this.Grid1.Children.Add(两个);
ToggleButton one=新的ToggleButton();
if(one==null)返回;
一、Content=“一”;
一、宽度=100;
一、高度=50;
一、余量=新厚度(0,0,250,0);
this.Grid1.Children.Add(一);
绑定=新绑定(“已检查”);
binding.Source=2;
binding.Mode=BindingMode.TwoWay;
一、设置绑定(ToggleButton.IsCheckedProperty,binding);
/*在UI中添加两个*/
}
}
}
您可以将两个按钮的IsChecked
属性绑定到ViewModel
中的同一属性
在main window.xaml中:
<ToggleButton Grid.Row="1"
Grid.Column="0"
Width="60"
Height="30"
Content="Button 1"
IsChecked="{Binding IsToggleButtonChecked}" />
<ToggleButton Grid.Row="1"
Grid.Column="1"
Width="60"
Height="30"
Content="Button 2"
IsChecked="{Binding IsToggleButtonChecked}" />
不要在代码中创建绑定,这太乱了。所以你想让切换按钮做与普通切换按钮相反的事情吗?为什么你需要一对按钮来保持相同?@paparazi它们在服务器上代表相同的对象,但在不同的菜单中,它们对相同的属性进行适当的绑定
private bool _isToggleButtonChecked = false;
public bool IsToggleButtonChecked
{
get { return _isToggleButtonChecked; }
set { Set<bool>(ref _isToggleButtonChecked, value); }
}