C# DateTime的StringFormat不适用于多重绑定
我有以下代码:C# DateTime的StringFormat不适用于多重绑定,c#,.net,wpf,multibinding,C#,.net,Wpf,Multibinding,我有以下代码: <Label> <Label.Content> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat="{} created on {0} by"> <Binding Path="CreationDate" StringFormat="{}
<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>
输出
我总是在2014年9月21日00:00:00通过
我尝试了StringFormat=“d”
,但也没有成功
我的代码有什么问题?您只有一个
绑定路径,因此您只能获得日期和时间。基本上,您需要为person数据类型添加一个绑定
元素。应该是这样的:
<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>
这相当于我在您的XAML中输入的内容:
string.Format("created on {0:dd/MM/yyyy} by ", someDate);
string.Format("created on {0:dd/MM/yyyy} by {1}", someDate, someEmployee.Name);
希望您现在可以看到差异。您只有一个绑定路径
,因此您只能获得日期和时间。请参阅MSDN上的页面,了解您应该做什么。基本上,您需要为您的person数据类型添加一个Binding
元素。您如何在MSDN上看到MultiBinding类页面???你点击链接。不管怎样,我现在为您添加了一个答案。它起作用了,但我不明白为什么将StringFormat属性放在MultiBinding元素上与第一个元素有不同的行为!就我所知,它没有。它被简单地移动到MultiBinding元素中,使用一个String.Format
,而不是两个。