WPF以编程方式绑定两种方式

时间:2016-06-22 15:51:34

标签: c# wpf binding

我正在尝试绑定一对ToggleButtons,所以当选中一个时,另一个也会被检查。

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不会切换。

2 个答案:

答案 0 :(得分:0)

您可以将两个按钮的IsChecked属性绑定到ViewModel中的同一媒体资源。

MainWindow.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}" />

MainViewModel.cs中(假设您可以使用MVVM Light):

  private bool _isToggleButtonChecked = false;
    public bool IsToggleButtonChecked
    {
        get { return _isToggleButtonChecked; }
        set { Set<bool>(ref _isToggleButtonChecked, value); }
    }

答案 1 :(得分:-1)

我认为您的代码没有任何问题。它似乎在我的结局完美。我已经在xaml和codebehind以及完全代码背后尝试过。

两者都按预期工作,Binding非常完美!

我在下面提供了一个示例。核实。绑定在我的最终完美!

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 */
        }
    }
}