2010-09-08 58 views
0

嘗試使單選按鈕綁定工作,但獲得下面的代碼運行時錯誤。希望單選按鈕一次只能選擇一個,並且它們以2種方式正確綁定。錯誤文字是。WPF單選按鈕綁定 - 在此代碼中出現錯誤?

<Window x:Class="testapp1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:l="clr-namespace:testapp1" Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.Resources> 
      <l:EnumBooleanConverter x:Key="enumBooleanConverter" /> 
     </Grid.Resources> 

     <StackPanel > 
      <RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=FirstSelection}">first selection</RadioButton> 
      <RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=TheOtherSelection}">the other selection</RadioButton> 
      <RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=YetAnotherOne}">yet another one</RadioButton> 
      <Label Content="{Binding Path=VeryLovelyEnum}" Height="28" Name="label1" /> 
     </StackPanel> 

    </Grid> 
</Window> 

和代碼 「上 類型 'testapp1.MainWindow' 那 匹配指定結合 約束引發了異常的構造方法的調用」:

namespace testapp1 
{ 
    public partial class MainWindow : Window 
    { 
     public TestModel _model; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      InitializeComponent(); 
      _model = new TestModel(); 
      this.DataContext = _model; 
     } 

    } 

    public enum MyLovelyEnum 
    { 
     FirstSelection, 
     TheOtherSelection, 
     YetAnotherOne 
    }; 


    public class TestModel : DependencyObject 
    { 

     public MyLovelyEnum VeryLovelyEnum 
     { 
      get { return (MyLovelyEnum)GetValue(VeryLovelyEnumProperty); } 
      set { SetValue(VeryLovelyEnumProperty, value); } 
     } 
     public static readonly DependencyProperty VeryLovelyEnumProperty = 
      DependencyProperty.Register("VeryLovelyEnum", typeof(MyLovelyEnum), typeof(TestModel), new UIPropertyMetadata(0)); 



    // from http://stackoverflow.com/questions/397556/wpf-how-to-bind-radiobuttons-to-an-enum 
    public class EnumBooleanConverter : IValueConverter 
    { 
     #region IValueConverter Members 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      string parameterString = parameter as string; 
      if (parameterString == null) 
       return DependencyProperty.UnsetValue; 

      if (Enum.IsDefined(value.GetType(), value) == false) 
       return DependencyProperty.UnsetValue; 

      object parameterValue = Enum.Parse(value.GetType(), parameterString); 

      return parameterValue.Equals(value); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      string parameterString = parameter as string; 
      if (parameterString == null) 
       return DependencyProperty.UnsetValue; 

      return Enum.Parse(targetType, parameterString); 
     } 
     #endregion 
    } 

} 

按鈕&標籤是否來自我之前的測試 - 我只是樂ft in ...

回答

2

以下聲明使用整數而不是默認枚舉值。可能是您的實例的問題...

public static readonly DependencyProperty VeryLovelyEnumProperty = 
      DependencyProperty.Register("VeryLovelyEnum", typeof(MyLovelyEnum), typeof(TestModel), new UIPropertyMetadata(0)); 

嘗試類似...

public static readonly DependencyProperty VeryLovelyEnumProperty = 
      DependencyProperty.Register("VeryLovelyEnum", typeof(MyLovelyEnum), typeof(TestModel), new UIPropertyMetadata(MyLovelyEnum.FirstSelection));