StringFormat 被忽略

发布于 2024-11-15 20:22:06 字数 446 浏览 4 评论 0原文


这是我的绑定(缩短,命令属性也被绑定)

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

ContectMenu 的 PlacementTarget 的标签属性是一个字符串,就像

"Short.Plural"

我期望在命令处理程序中收到的是:

Key: Short.Plural

但我实际收到的是:

Short.Plural

This is my binding (shortened, Command-Property is also bound)

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

The Tag-Property of ContectMenu's PlacementTarget is a String like

"Short.Plural"

What i expect to receive in the Command-Handler is:

Key: Short.Plural

But what i acutally receive is:

Short.Plural

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就是爱搞怪 2024-11-22 20:22:06

Label 不使用 StringFormat,而是使用 ContentStringFormat。这样使用它:

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

Label does not use StringFormat but ContentStringFormat. Use it this way:

<TextBlock x:Name="textBlock" Text="Base Text"/>
<Label Content="{Binding Path=Text, ElementName=textBlock}" ContentStringFormat="FORMATTED {0}"/>
赏烟花じ飞满天 2024-11-22 20:22:06

我很惊讶,但我的测试仅表明 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

I'm astounded, but my tests just show that StringFormat only applies if the target d-prop is of type String. I've never noticed this before, nor heard it mentioned. I don't have more time to look into it right now, but this seems ridiculous.

Seriously, this works:

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

This does not:

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

Since Label.Content is not a String.

月棠 2024-11-22 20:22:06

使用 Binding Converter:

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>

在菜单命令参数绑定中引用它:

<MenuItem Header="Key" CommandParameter="{Binding Converter={StaticResource commandParamConverter}, Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>

use Binding Converter:

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();
    }
}

Add it to Windows\UserControl resource:

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

Refer it in Menu CommandParameter binding:

<MenuItem Header="Key" CommandParameter="{Binding Converter={StaticResource commandParamConverter}, Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文