2009-10-29 5 views
1

WPF TwoWay 데이터 바인딩 이 포커스가없는 컨트롤에서 작동한다고 가정하는 것이 안전합니까?WPF TwoWay 데이터 바인딩 제한

예를 들어 다음 코드를 참조하십시오.

<Window.Resources> 
     <XmlDataProvider x:Key="TestBind1" XPath="/BindTest1"> 
      <x:XData> 
       <BindTest1 xmlns=""> 
        <Value1>True</Value1> 
       </BindTest1> 
      </x:XData> 
     </XmlDataProvider> 
    </Window.Resources> 
    <StackPanel> 
     <GroupBox> 
      <StackPanel> 
       <RadioButton Content="Value1" IsChecked="{Binding Source={StaticResource TestBind1},Mode=TwoWay, XPath=Value1}"/> 
       <RadioButton Content="Value2"/> 
      </StackPanel> 
     </GroupBox> 
     <Button Content="Analyse" Click="OnAnalyseClicked"/> 

    </StackPanel> 

내가 라디오 버튼을 클릭하면 라디오 버튼 값 1에 포커스가 didnt는 그 동안 변경 되었기 때문에 값 2가, BindTest1/값 1의 값이 사실 유지됩니다?

WPF에서는 이러한 현상이 발생합니까? 나는 다양한 기술을 사용하여 이것을 피할 수 있다는 것을 알고 있지만, 이것이 정상적인 것인지 또는이 문제를 일으키는 매개 변수가 Xaml에없는 것인지 묻고 싶습니다.

답변

1

마지막으로, 답변을 찾았습니다. 기본적으로, 매번 당신은 그것의 바인딩을 변경하기 때문에 RadioButtons를위한 휴식, 바인딩의 라디오 버튼은 나는의 RadioButton을 전문으로하고 바인딩을 방지 할 수있는 대답 here을 발견

그룹의 다른 버튼의 체크 상태를 변경합니다 변경되고있다.

바인딩을 수정하는 데 사용한 샘플 클래스.

/// <summary> 
    /// data bound radio button 
    /// </summary> 
    public class DataBoundRadioButton : RadioButton 
    { 
     /// <summary> 
     /// Called when the <see cref="P:System.Windows.Controls.Primitives.ToggleButton.IsChecked"/> property becomes true. 
     /// </summary> 
     /// <param name="e">Provides data for <see cref="T:System.Windows.RoutedEventArgs"/>.</param> 
     protected override void OnChecked(RoutedEventArgs e) 
     { 
      // Do nothing. This will prevent IsChecked from being manually set and overwriting the binding. 
     } 

     /// <summary> 
     /// Called by the <see cref="M:System.Windows.Controls.Primitives.ToggleButton.OnClick"/> method to implement a <see cref="T:System.Windows.Controls.RadioButton"/> control's toggle behavior. 
     /// </summary> 
     protected override void OnToggle() 
     { 
      BindingExpression be = GetBindingExpression(RadioButton.IsCheckedProperty); 
      Binding bind = be.ParentBinding; 

      Debug.Assert(bind.ConverterParameter != null, "Please enter the desired tag as the ConvertParameter"); 

      XmlDataProvider prov = bind.Source as XmlDataProvider; 
      XmlNode node = prov.Document.SelectSingleNode(bind.XPath); 
      node.InnerText = bind.ConverterParameter.ToString(); 

     } 
    } 
1

바인딩 은 컨트롤에 포커스가 있는지 여부에 관계없이 업데이트됩니다. 내 생각 엔 XAML에서 뭔가 다른 점이 있다는 것입니다.