0

このスタイルを使用すると自動的にコンバーターが使用されるように、コンバーターをアタッチするスタイルを作成しようとしています。私が抱えている問題は、スタイルにパスを設定しないと、コンパイラが気に入らないことです。デザイン時にパスを選択したいので、バインディングの「パス」プロパティをスタイルに設定したくありません。すべてのコントロールが自動的に同じパス名を使用するわけではありません。

これが私の例です:

<Style x:Key="SomeCustomTextBox" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
    <Setter Property="Text">
        <Setter.Value>
            <Binding>
                <Binding.Path>SomePath</Binding.Path>
                <Binding.Converter>
                    <Converters:SomeIValueConverter/>
                </Binding.Converter>
            </Binding>
        </Setter.Value>
    </Setter>
</Style>

また、xaml コードの次の行 (ここでは以下) で同様のスタイルを使用すると、バインディング パスだけでなく、バ​​インディング全体が自動的に上書きされます。

<TextBox Height="28" Name="someNameThatOveridesDefaultPath" Style="{StaticResource SomeCustomTextBox}" MaxLength="5" />

どういうわけかこのようなことは可能ですか?

ありがとう!パトリック・ミロン

4

1 に答える 1

0

このような添付プロパティを使用してみてください

public class AttachedProperties
{
    public static readonly DependencyProperty RawTextProperty = DependencyProperty.RegisterAttached(
        "RawText",
        typeof(string),
        typeof(AttachedProperties));

    public static void SetRawText(UIElement element, string value)
    {
        element.SetValue(RawTextProperty, value);
    }

    public static string GetRawText(UIElement element)
    {
        return element.GetValue(RawTextProperty) as string;
    }
}

次に、XAMLで

<Style x:Key="SomeCustomTextBox" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
    <Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Mode=Self}, 
    Path=local:AttachedProperties.RawText, Converter=Converters:SomeIValueConverter}">
    </Setter>
</Style>


<Grid>
    <TextBox Style="{StaticResource SomeCustomTextBox}" 
                local:AttachedProperties.RawText="test text" />
</Grid>

私はコードをテストしていませんが、これはアイデアです

于 2012-06-15T14:56:07.347 に答える