2012-10-14 40 views
0

WPF/Caliburn微相關問題。通過代碼切換綁定單選按鈕

我有4個單選按鈕,我將IsChecked屬性綁定到ArrowType,它具有一種我創建的LogicArrowEnum枚舉類型。

Radiobuttons使用轉換器將相關枚舉正確地分配給基於哪個按鈕被單擊的ArrowType屬性。

XAML:

<Window.Resources> 
    <my:EnumToBoolConverter x:Key="EBConverter"/> 
</Window.Resources> 

... 

<RadioButton IsChecked="{Binding ArrowType, 
       Converter={StaticResource EBConverter}, 
       ConverterParameter={x:Static my:LogicArrowEnum.ARROW}}" 
         Name="LogicArrow" 
         Style="{StaticResource {x:Type ToggleButton}}" 
         Width="50" 
       <TextBlock Text="Arrow"/> 
</RadioButton> 
<RadioButton IsChecked="{Binding ArrowType, 
       Converter={StaticResource EBConverter}, 
       ConverterParameter={x:Static my:LogicArrowEnum.ASSIGN}}" 
       Name="LogicAssign" 
       Style="{StaticResource {x:Type ToggleButton}}" 
       Width="50" 
       <TextBlock Text="Assign"/> 
</RadioButton> 
<RadioButton 
       IsChecked="{Binding ArrowType, 
       Converter={StaticResource EBConverter}, 
       ConverterParameter={x:Static my:LogicArrowEnum.IF}}" 
       Name="LogicIf" 
       Style="{StaticResource {x:Type ToggleButton}}" 
       Width="50" 
       <TextBlock Text="If" /> 

代碼:

public class EnumToBoolConverter : IValueConverter 
{ 
    public object Convert(object value, 
     Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     if (parameter.Equals(value)) 
      return true; 
     else 
      return false; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return parameter; 
    } 
} 

public enum LogicArrowEnum 
{ 
    ARROW = 1, 
    ASSIGN = 2, 
    IF = 3, 
    IF_ELSE = 4 
} 

public LogicArrowEnum ArrowType 
{ 
    get { return arrowType; } 
    set 
    { 
     arrowType = value; 

     NotifyOfPropertyChange(() => ArrowType); 
    } 
} 

代碼奇妙的作品 - 用戶點擊一個按鈕,該ArrowType屬性正確綁定。

我想使這項工作也向後。例如,如果我通過代碼將ArrowType屬性設置爲LogicArrowEnum.ASSIGN,則UI應該顯示「分配」按鈕已被切換。出於某種原因,這不符合預期。在屬性的set方法中,無論何時將ArrowType屬性分配給任意枚舉,arrowType的私有字段都將首先分配爲我希望的值,但只要代碼達到NotifyOfPropertyChange方法,它就會使其進入再次設置方法,但隨後將arrowType專用字段重置爲先前切換的按鈕。

這是一個Caliburn微相關的錯誤或一些WPF相關的錯誤?我怎樣才能解決這個問題?

回答

1

與Caliburn.Micro沒有任何關係。 看看這個:How to bind RadioButtons to an enum?

從斯科特答案採取轉換器,它會正常工作。

+0

我試過這個,但我遇到了同樣的問題。你絕對肯定與Caliburn Micro沒有關係嗎?由於NotifyOfPropertyChange,屬性的setter會激活兩次。 – l46kok

+0

啊,沒關係,我只是注意到你提到了一個不同於接受的回答者的名字。謝謝! – l46kok