2016-06-22 3 views
-2

ToggleButtons 한 쌍을 바인딩하려고하는데, 하나를 선택하면 다른 쪽도 선택됩니다.WPF Programaticlly 바인딩 양방향

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은 토글되지 않습니다.

+3

코드에서 바인딩을 작성하지 마십시오, 그것은 엉망입니다. –

+0

그래서 토글 버튼이 정상적인 토글 버튼의 ​​기능과 반대가되도록하려면? 왜 같은 단추를 사용해야합니까? – Paparazzi

+0

@Paparazzi 그들은 서버의 동일한 객체를 나타내지 만 다른 메뉴의 – KevinA

답변

0

당신은 당신의 ViewModel에 동일한 속성에 두 버튼의 IsChecked 속성을 바인딩 할 수 있습니다.

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

코드에 문제가 있다고 생각하지 않습니다. 그것은 내 끝 부분에서 완벽하게 작동하는 것 같습니다. xaml과 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 */ 
     } 
    } 
}