25


これは私のバインディングです(短縮、Command-Propertyもバインドされています)

<MenuItem Header="Key" CommandParameter="{Binding StringFormat='Key: {0}', Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>

ContectMenuのPlacementTargetのTag-Propertyは、次のような文字列です。

"Short.Plural"

コマンドハンドラーで受け取ることを期待しているのは次のとおりです。

Key: Short.Plural

しかし、私が実際に受け取るのは次のとおりです。

Short.Plural
4

3 に答える 3

35

LabelはStringFormatではなくContentStringFormatを使用します。このように使用します。

<TextBlock x:Name="textBlock" Text="Base Text"/>
<Label Content="{Binding Path=Text, ElementName=textBlock}" ContentStringFormat="FORMATTED {0}"/>
于 2012-11-15T16:05:56.257 に答える
24

私は驚いていますが、私のテストでStringFormatは、ターゲットのd-propがタイプの場合にのみ適用されることが示されていますString。私はこれまでこれに気づいたことも、言及されたことも聞いたことがありません。今は調べる時間がありませんが、これはばかげているようです。

真剣に、これは機能します:

<TextBlock x:Name="textBlock" Text="Base Text"/>
<TextBlock Text="{Binding StringFormat=FORMATTED {0}, Path=Text, ElementName=textBlock}"/>

これはしません:

<TextBlock x:Name="textBlock" Text="Base Text"/>
<Label Content="{Binding StringFormat=FORMATTED {0}, Path=Text, ElementName=textBlock}"/>

Label.ContentではないのでString

于 2011-06-16T10:43:19.207 に答える
1

バインディングコンバーターを使用します。

public class CommandParamConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string)
        {
            return string.Format("Key {0}", value);
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Windows\UserControlリソースに追加します。

<Window.Resources>
    <local:CommandParamConverter x:Key="commandParamConverter" />
</Window.Resources>

MenuCommandParameterバインディングで参照してください。

<MenuItem Header="Key" CommandParameter="{Binding Converter={StaticResource commandParamConverter}, Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
于 2011-06-16T11:35:22.163 に答える