如何使用Value Converter for Listbox,textblock作为datatemplate [英] How to use Value Converter for Listbox , textblock as datatemplate
问题描述
HI,
我的列表框如下所示,其中textblock作为项目模板中的datatemplate,我有observablecollection类型字符串,我想在Listbox中显示我正在使用Ivalueconverter将其转换为Textblock,因为我需要说一些字Bold
I have listbox as below which has textblock as datatemplate inside item template, I have observablecollection type string which I want to show in Listbox for which I am converting that to Textblock using Ivalueconverter, as I need to make some word Bold
<ListBox x:Name="chatListBoxMsgs">
<ListBox.Resources>
<viewmodel:convert x:Key="PositionConverter"/>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource PositionConverter}}"/>
<TextBlock/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
从后面的代码我绑定它如下,
From code behind I am binding it as follow,
ObservableCollection<string> MessageHistory = new ObservableCollection<string>();
MessageHistory.Add("Agent: how can I help you");
MessageHistory.Add("Customer: I need to get this");
chatListBoxMsgs.ItemsSource = MessageHistory;
我需要收转代理:和客户:以上收藏中的大胆,所以我使用的是Ivalueconverter,如下所示
I need to conver "Agent:" and "customer:" bold in above collection so I am using Ivalueconverter as below
public class convert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
TextBlock _tbl= new TextBlock();
//Here I have code to make Agent bold, and then return
return _tbl;
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我的问题是我我没有在Listbox中获取消息而是获取system.control.textblock。我需要在Listbox中以粗体显示上面的字符串
这就是我使用ConverterClass的方式。
My problem is that I am not getting the message in Listbox instead getting system.control.textblock. I need to display above string with Agent in bold in Listbox
This is how I have used ConverterClass.
<ListBox.Resources>
<viewmodel:convert x:Key="PositionConverter"/>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource PositionConverter}}"/>
<TextBlock/>
</DataTemplate>
</ListBox.ItemTemplate>
我需要帮助。可能是我无法绑定,提前谢谢
I need help here. May be I am not able to bind, Thanks in advance
推荐答案
你一定不能使用TextBlock并绑定到Text,因为这最终会将结果转换为String。
使用ContentPresenter代替,而不是
Hi,
you must not use TextBlock and bind to Text, since this will ultimately convert the result to a String.
Use ContentPresenter instead, i.e. instead of
<datatemplate>
<textblock text="{Binding Converter={StaticResource PositionConverter}}"></textblock>
<textblock></textblock>
</datatemplate>
使用
use
<datatemplate>
<ContentPresenter Content="{Binding Converter={StaticResource PositionConverter}}"></ContentPresenter>
<textblock></textblock>
</datatemplate>
这篇关于如何使用Value Converter for Listbox,textblock作为datatemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!