Ger*_*nck 80 .net wpf datatemplate
我想在WPF ItemsControl中显示Customer对象列表.我为此创建了一个DataTemplate:
<DataTemplate DataType="{x:Type myNameSpace:Customer}">
<StackPanel Orientation="Horizontal" Margin="10">
<CheckBox"></CheckBox>
<TextBlock Text="{Binding Path=Number}"></TextBlock>
<TextBlock Text=" - "></TextBlock>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
所以我想要的基本上是一个包含NUMBER - NAME的简单列表(带复选框).有没有办法可以在绑定部分直接连接数字和名称?
PiR*_*iRX 159
有StringFormat属性(在.NET 3.5 SP1中),您可以使用它.有用的WPF绑定作弊可以在这里找到.如果它没有帮助,您可以始终为您的对象编写自己的ValueConverter或自定义属性.
刚刚检查过,你可以使用StringFormat进行多重绑定.在你的情况下代码将是这样的:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat=" {0} - {1}">
<Binding Path="Number"/>
<Binding Path="Name"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
我不得不用空格开始格式化字符串,否则Visual Studio不会构建,但我认为你会找到解决它的方法:)
编辑
StringFormat中需要空格以防止解析器被{0}视为实际绑定.其他替代品:
<!-- use a space before the first format -->
<MultiBinding StringFormat=" {0} - {1}">
<!-- escape the formats -->
<MultiBinding StringFormat="\{0\} - \{1\}">
<!-- use {} before the first format -->
<MultiBinding StringFormat="{}{0} - {1}">
Run Code Online (Sandbox Code Playgroud)
小智 60
如果您想要使用静态文本连接动态值,请尝试以下操作:
<TextBlock Text="{Binding IndividualSSN, StringFormat= '\{0\} (SSN)'}"/>
Run Code Online (Sandbox Code Playgroud)
显示:234-334-5566(SSN)
小智 8
请参阅我在使用Run类的代码中使用的以下示例:
<TextBlock x:Name="..." Width="..." Height="..."
<Run Text="Area="/>
<Run Text="{Binding ...}"/>
<Run Text="sq.mm"/>
<LineBreak/>
<Run Text="Min Diameter="/>
<Run Text="{Binding...}"/>
<LineBreak/>
<Run Text="Max Diameter="/>
<Run Text="{Binding...}"/>
</TextBlock >
Run Code Online (Sandbox Code Playgroud)