11

コンバーターを使用して、テキスト ボックスを double のリストにバインドする wpf ユーザー コントロールを実装しようとしています。ユーザーコントロールのインスタンスをコンバーターパラメーターに設定するにはどうすればよいですか?

コントロールのコードを以下に示します

ありがとう

<UserControl x:Class="BaySizeControl.BaySizeTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="clr-namespace:BaySizeControl"
    >
    <UserControl.Resources>
        <local:BayListtoStringConverter x:Key="BaySizeConverter"/>
    </UserControl.Resources>
    <Grid>

        <TextBox  Name="Textbox_baysizes" 
                  Text="{Binding RelativeSource={RelativeSource self},
                                Path=Parent.Parent.BaySizeItemsSource, 
                                Converter={StaticResource BaySizeConverter}}"
                  />
    </Grid>
</UserControl>
4

4 に答える 4

11

もう 1 つの方法は、コンバーターを DependencyObject (または FrameworkElement) から継承させることです。これにより、依存関係プロパティを宣言できるようになり、その値を XAML (Binding であっても) から設定できるようになります。

例: カスタム コントロール (MyControl) 内のプロパティ (FactorValue) から取得した係数を指定する値を乗算するコンバーター。

コンバーター:

public class MyConverter : DependencyObject, IValueConverter
{
    // The property used as a parameter
    public double Factor
    {
        get { return (double) GetValue(FactorProperty); }
        set { SetValue(FactorProperty, value); }
    }

    // The dependency property to allow the property to be used from XAML.
    public static readonly DependencyProperty FactorProperty =
        DependencyProperty.Register(
        "Factor",
        typeof(double),
        typeof(MyConverter),
        new PropertyMetadata(1.15d));

    #region IValueConverter Members

    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // Use the property in the Convert method instead of "parameter"
        return (double) value * Factor;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

XAML での使用:

<MyConverter x:Key="MyConv"
             Factor={Binding ElementName=MyControl, Path=FactorValue}
/>

そのため、コンバーターで必要な各パラメーターの依存関係プロパティを宣言してバインドできるようになりました。

于 2011-11-16T11:56:33.793 に答える
8

パラメーターは、コンバーターが必要とする定数用です。コンバーターにオブジェクト インスタンスを提供するには、MultiBinding を使用できます。

注: このソリューションを機能させるには、IValueConverter の代わりに IMultiValueConverter を実装するようにコンバーターを変更する必要もあります。幸いなことに、関連する変更はごくわずかです。コンバーターに提供される値の数の検証を追加できます。この場合は 2 です。

<TextBox Name="Textbox_baysizes">
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource BaySizeConverter}">
            <Binding RelativeSource="{RelativeSource self}" Path="Parent.Parent.BaySizeItemsSource"/>
            <Binding ElementName="Textbox_baysizes"/>
        </MultiBinding>
    </TextBox.Text>
</TextBox>
于 2008-12-18T13:40:29.703 に答える
1

同じ問題がありましたが、ConvertBack メソッドを正しく実装する必要があるため、MultiBindings を使用できません。これが、CheckBox の IsChecked プロパティに実装することになった解決策です。

<CheckBox>
    <CheckBox.IsChecked>
        <Binding Converter="{StaticResource myConverter}" Path="Value">
            <Binding.ConverterParameter>
                <FrameworkElement DataContext="{TemplateBinding DataContext}" />
            </Binding.ConverterParameter>
        </Binding>
    </CheckBox.IsChecked>
</CheckBox>

私は TemplateBindings (またはそのことについては WPF) にあまり精通していないので、CheckBox が DataTemplate にあるためにのみ機能する可能性があります...

于 2010-09-21T21:22:59.487 に答える
1

コントロールに名前を付けてから、ElementName を使用してバインドします。

<UserControl x:Class="BaySizeControl.BaySizeTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="clr-namespace:BaySizeControl"
    Name="Foobar"
    >
    <UserControl.Resources>
        <local:BayListtoStringConverter x:Key="BaySizeConverter"/>
    </UserControl.Resources>
    <Grid>

        <TextBox  Name="Textbox_baysizes" 
                  Text="{Binding RelativeSource={RelativeSource self},
                                Path=Parent.Parent.BaySizeItemsSource, 
                                Converter={StaticResource BaySizeConverter,
                                ConverterParameter={Binding ElementName=Foobar} }}"
                  />
    </Grid>
</UserControl>

いいえ、待ってください。ConverterParameter は Dependency Property ではなく、Binding も DependencyObject ではないため、機能しません。ReleativeSource マークアップ拡張機能は、他の MarkupExtension 内にネストして使用したことはありませんが、必要に応じて実行する必要があります。おそらく、この場合は適切に動作していません。

<TextBox  Name="Textbox_baysizes" 
                      Text="{Binding RelativeSource={RelativeSource self},
                                    Path=Parent.Parent.BaySizeItemsSource, 
                                    Converter={StaticResource BaySizeConverter,
                                    ConverterParameter={RelativeSource self} }}"
                      />
于 2008-12-18T13:47:05.680 に答える