0

假设我有一个带有资源键GroupBoxHeaderCaption和值“SomeString”的 .resx 文件。

然后在我的ViewModel中有一个string名为Description.

我想要实现的是这个(假设 .resx 文件被引用using resx = [...]并且视图模型被调用viewModel):

string.Format("{0}: {1}", resx.GroupBoxHeaderCaption, viewModel.Description)

是否可以在 XAML 中执行此操作?我得到了这个,但它不起作用:

    <GroupBox Margin="4">
        <GroupBox.HeaderTemplate>
            <DataTemplate>
                <Label>
                    <Label.Content>
                        <MultiBinding StringFormat="{}{0}: {1}">
                            <Binding Path="{x:Static my:MyResources.GroupBoxHeaderCaption}" />
                            <Binding Path="viewModel.Description" />
                        </MultiBinding>
                    </Label.Content>
                </Label>
            </DataTemplate>
        </GroupBox.HeaderTemplate>

通过不工作,我的意思是我得到GroupBoxHeaderCaption红色下划线,并显示错误:

无效的成员类型:预期类型为“PropertyPath”,实际类型为“字符串”。

我知道我可以为我viewModel.Description的 .


当我这样做时,我得到了想要的结果:

<GroupBox Margin="4" Header="{Binding viewModel.Description}" 
                     HeaderStringFormat="SomeString: {0}">

我想从 .resx 文件中获取“SomeString:”部分。

4

1 回答 1

2

我刚刚弄清楚你为什么会收到这个错误。这是因为您不能在stringthat 的Path属性中引用这样的资源MultiBinding

我认为也许你必须尝试这样的事情:

<MultiBinding StringFormat="{}{0}: {1}">
    <Binding Path="GroupBoxHeaderCaption" Source="{x:Static my:MyResources}" />
    <Binding Path="viewModel.Description" />
</MultiBinding>

不幸的是,我现在不能尝试这个,我不确定这是否是正确的语法,所以如果你还有其他问题,请回来告诉我。

于 2013-10-28T14:07:01.090 回答