ParityTypeSelect
2 つの RadioButton - Odd
&を含むUserControl を実装しようとしましたが、UserControl は双方向バインディングで使用されるEven
DependencyProperty を呼び出す必要があります。ParityType
アイデアは単純です - が選択された場合Odd
はParityType
1 を返し、Even
選択された場合はParityType
0 を返す必要が
あります。コードは次の とおりです。
XAML (ユーザー コントロール):
<StackPanel Orientation="Horizontal">
<RadioButton Name="rdoOdd" Content="Odd" Margin="5" Checked="rdoOdd_CheckedChnaged" Unchecked="rdoOdd_CheckedChnaged" />
<RadioButton Name="rdoEven" Content="Even" Margin="5"/>
</StackPanel>
コード ビハインド (ユーザー コントロール):
public partial class ParityTypeSelect : UserControl
{
//Some Code
static ParityTypeSelect()
{
FrameworkPropertyMetadata parityTypeMetaData =
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnParityTypeChanged),
new CoerceValueCallback(CoerceParityTypeValue));
ParityTypeProperty = DependencyProperty.Register("ParityType", typeof(int?), typeof(ParityTypeSelect),
parityTypeMetaData,
new ValidateValueCallback(ValidateParityTypeValue));
}
public static readonly DependencyProperty ParityTypeProperty;
public int? ParityType
{
get { return (int?)GetValue(ParityTypeProperty); }
set { SetValue(ParityTypeProperty, value); }
}
private static void OnParityTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ParityTypeSelect select = (ParityTypeSelect)d;
int? newValue = (int?)e.NewValue;
if (newValue != null && newValue <= 1)
{
if (newValue == 1)
select.rdoOdd.IsChecked = true;
else
select.rdoEven.IsChecked = true;
}
else
return;
}
private void rdoOdd_CheckedChnaged(object sender, RoutedEventArgs e)
{
RadioButton radioButton = (RadioButton)sender;
if (radioButton.IsChecked != null)
{
if (radioButton.IsChecked.Value)
SetValue(ParityTypeProperty, 1);
else
SetValue(ParityTypeProperty, 0);
}
}
//Some more Code
}
XAML (消費者):
<StackPanel>
<aucl:ParityTypeSelect ParityType="{Binding Path=Format.FisrtBitType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
...そしてそれは働いています。しかし、問題は、これが DependencyProperty の私の最初の実装だったということです。なので、ちゃんとやっているか心配です。OnParityTypeChanged
メソッドを正しい方法で使用していますか? rdoOdd_CheckedChnaged
イベントハンドラーを使用してプロパティ値を設定しても問題ありませんか? この実装全体のいずれかを行うためのより良い、またはより適切な方法はありますか? 私は常に高品質のコーディングを楽しみにしています。したがって、提案、改善の推奨事項、WPF
みんなからのコメントはありがたいです。