2

ParityTypeSelect2 つの RadioButton - Odd&を含むUserControl を実装しようとしましたが、UserControl は双方向バインディングで使用されるEvenDependencyProperty を呼び出す必要があります。ParityTypeアイデアは単純です - が選択された場合OddParityType1 を返し、Even選択された場合はParityType0 を返す必要が
あります。コードは次の とおりです。

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みんなからのコメントはありがたいです。

4

2 に答える 2

1

これをコメントに書こうとしていましたが、別の回答として行います。コードビハインドで行うのではなく、プロパティをバインドします。int? bool に変換する必要があります。コンバーターを可能な限り汎用的にするには、次のEqualityConverterようにバインドできる を使用します。

<RadioButton Content="Odd" Margin="5" IsChecked="{Binding ParityTypeSelect,Mode=TwoWay,Converter={StaticResource equalityConverter}, ConverterParameter=1}" />

コンバーターのコードは次のとおりです。

public class EqualityConverter : IValueConverter
{
    public object TrueValue { get; set; }
    public object FalseValue { get; set; }

    public EqualityConverter()
    {
        //default the TrueValue and FalseValue to true and false.
        //this way we can easily use the same converter for simple comparison or as an IIF statement
        TrueValue = true;
        FalseValue = false;
    }
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null && parameter == null) return TrueValue;
        if (value == null && parameter != null) return FalseValue;

        //in some cases we might need to compare an enum value to an integer.
        //this will fail unless we specifically convert them
        if (value is int && parameter is Enum)
            parameter = System.Convert.ToInt32(parameter);
        else if (value is Enum && parameter is int)
            value = System.Convert.ToInt32(value);

        return value.Equals(parameter) ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null && TrueValue == null) return true;
        if (value == null && FalseValue == null) return false;
        if (value.Equals(TrueValue)) return true;
        return false;
    }
}

私が言う他のいくつかのこと:-ユーザーコントロールを使用する代わりに、コントロールから継承する方が良いです(標準クラスとして、xamlはありません)。次に、xaml を Generic.xaml というファイルに入れます。関連するものが少しあるので、詳細を検索する必要があります.CoerceParityTypeValueとValidateParityTypeValueへの不必要な呼び出しがあります.これらは省略できます. - 依存プロパティを定義するときは、「propdp」(引用符なし) と入力してタブを押します - 通常、コントロールのコントロールは PART_ で始まる名前が付けられます (例: PART_OddButton)

編集: これは主要なポイントを示しているように見えるが完全ではない記事です: http://wpftutorial.net/HowToCreateACustomControl.html

于 2012-07-19T01:39:22.073 に答える
0

ただし、変更されたイベントコードでラジオボタンの値を設定するのではなく、xaml のようにそれらをバインドすることができます。

<RadioButton Name="rdoOdd" Content="Odd" Margin="5" IsChecked="{Binding ParityTypeSelect,Mode=TwoWay,Converter=StaticResource booltoNullIntConverter }" />

booltoNullIntConverter は、要件に応じて bool を null int に変更するコンバーターです。これが役立つことを願っています。

于 2012-07-19T00:55:27.540 に答える