お世話になっております。
ユーザーコントロールに配置したラジオボタンと列挙型を双方向バインドさせたいと思っております。
こちらのC#のWPFでRadioButtonのIsCheckedに列挙型をバインドするを参考にして
MainWindowに配置したラジオボタンとViewModelクラスを双方向バインドさせることはできたのですが、
ユーザーコントロールに配置したラジオボタンに対してバインドさせたいと思っています。
ユーザーコントロール側に依存関係プロパティ、ラッパーを記述して、
MainWindowから「列挙型をBooleanへ変換するコンバータ」を経由させてバインドさせよう等試してみましたが、
うまく行きませんでした。
どのように実装を行えばよいのでしょうか。
よろしくお願いします。
環境
C#, .NET F/W 4.5, VS2017 Pro
--追記
XAML側
C#
1<UserControl x:Class="WpfApp1.RadioButtonUC" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 d:DesignHeight="300" d:DesignWidth="300" 9 Name="root"> 10 <UserControl.Resources> 11 <ResourceDictionary> 12 <local:EnumToBooleanConverter x:Key="EnumToBoolean"/> 13 </ResourceDictionary> 14 </UserControl.Resources> 15 16 <StackPanel> 17 <RadioButton Content="UC Tyep1" 18 IsChecked="{Binding TypeChange,ElementName=root,Converter={StaticResource EnumToBoolean},ConverterParameter=TYPE1,UpdateSourceTrigger=PropertyChanged}"/> 19 <RadioButton Content="UC Tyep1" 20 IsChecked="{Binding TypeChange,ElementName=root,Converter={StaticResource EnumToBoolean},ConverterParameter=TYPE2,UpdateSourceTrigger=PropertyChanged}"/> 21 <RadioButton Content="UC Tyep1" 22 IsChecked="{Binding TypeChange,ElementName=root,Converter={StaticResource EnumToBoolean},ConverterParameter=TYPE3,UpdateSourceTrigger=PropertyChanged}"/> 23 </StackPanel> 24</UserControl>
コード側
C#
1public partial class RadioButtonUC : UserControl 2 { 3 public RadioButtonUC() 4 { 5 InitializeComponent(); 6 } 7 8 //--- 依存関係プロパティ 9 public static readonly DependencyProperty TypeChangeProperty = 10 DependencyProperty.Register( 11 "TypeChange", 12 typeof(bool), 13 typeof(RadioButtonUC), 14 new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 15 16 //--- CLRプロパティ ラップ 17 public bool TypeChage 18 { 19 get { return (bool)GetValue(TypeChangeProperty); } 20 set { SetValue(TypeChangeProperty, value); } 21 } 22 }
ソースコードはこのようになっています。
上記のコードでは例外が投げられてしまいます。
System.Windows.Markup.XamlParseException: ''プロパティ'System.Windows.Controls.Primitives.ToggleButton.IsChecked' の Set で例外がスローされました。' 行番号 '18'、行位置 '22'。'
¥

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/01 03:00