DateTime的StringFormat不适用于MultiBinding

Was*_*RAR 2 .net c# wpf multibinding

我有这个代码:

<Label>
    <Label.Content>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{} created on {0} by">
                    <Binding Path="CreationDate" StringFormat="{}{0:dd/MM/yyyy}" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </LabeledLabel.Content>
</Label>
Run Code Online (Sandbox Code Playgroud)

OUTPUT

我总是得到这个 created on 21/09/2014 00:00:00 by

我试过了StringFormat="d",但它也没用.

我的代码有什么问题?

She*_*dan 5

你只有一个Binding Path,所以你只能得到日期和时间.基本上,您需要Binding为个人数据类型添加元素.它应该更像是这样的:

<Label>
    <Label.Content>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{} created on {0:dd/MM/yyyy} by {1}">
                    <Binding Path="CreationDate" />
                    <Binding Path="SomeEmployeeObject.Name" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </LabeledLabel.Content>
</Label>
Run Code Online (Sandbox Code Playgroud)

请注意,您也可以设置DateTime StringFormat使用MultiBinding.StringFormat属性,而不是在第一个Binding对象上添加另一个属性.您还需要将其添加{1}到结尾,MultiBinding.StringFormat以便输出第二个(与人相关)值.

有关详细信息,请参阅MSDN上的MultiBinding类页面.


更新>>>

我不明白为什么将StringFormat属性放在MultiBinding元素上与第一个元素相比具有不同的行为

它没有......我本可以把它留在那里,但我之所以移动它是因为你已经使用了它StringFormat.StringFormat在a上使用property MultiBinding实际上与使用该string.Format方法相同.使用该方法,这相当于您在XAML中的内容:

string.Format("created on {0:dd/MM/yyyy} by ", someDate);
Run Code Online (Sandbox Code Playgroud)

这相当于我在XAML中的内容:

string.Format("created on {0:dd/MM/yyyy} by {1}", someDate, someEmployee.Name);
Run Code Online (Sandbox Code Playgroud)

希望您现在可以看到差异.