简体   繁体   English

如何对齐 DataTemplateSelector 选择的 DataTemplate 中的文本?

[英]How to align Text in DataTemplate choosen by a DataTemplateSelector?

I am trying to align text in a DataTemplate of a ListBox depending on the choice of the DataTemplateSelector.我试图根据 DataTemplateSelector 的选择在 ListBox 的 DataTemplate 中对齐文本。

What I want is something like this:我想要的是这样的: 在此处输入图片说明

And tried to use a DataTemplateSelector并尝试使用 DataTemplateSelector

    <DataTemplate x:Key="RequestTemplate" DataType="local:Message">
            <TextBlock Text="{Binding Text}" Background="LightGreen" TextAlignment="Left" TextWrapping="Wrap"/>
    </DataTemplate>

    <DataTemplate x:Key="ResponseTemplate" DataType="local:Message" >
            <TextBlock Text="{Binding Text}"  Background="Yellow" TextAlignment="Right" TextWrapping="Wrap"/>
    </DataTemplate>

    <local:MesssageDataTemplateSelector x:Key="MessageDataTemplateSelector"
          ResponseTemplate="{StaticResource ResponseTemplate}"
          RequestTemplate="{StaticResource RequestTemplate}" />

The ListBox itself:列表框本身:

<ListBox ItemTemplateSelector="{StaticResource MessageDataTemplateSelector}"  ItemsSource="{Binding Messages}" />

The colors are applied the alignment is not.应用了颜色,但未应用对齐方式。

I also tried HorizontalAlignment, also not working.我也试过 Horizo​​ntalAlignment,也没有用。 How to accomplish the effect?效果如何实现?

One solution is to change the ListBoxItem style so that its HorizontalContentAlignment is Stretch :一种解决方案是更改ListBoxItem样式,使其HorizontalContentAlignmentStretch

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListBox.ItemContainerStyle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# 中为自定义 DataTemplateSelector 获取 DataTemplate 的 {x:DataType} - How to get {x:DataType} for a DataTemplate in C# for custom DataTemplateSelector 引用父DataTemplateSelector的DataTemplateSelector中的DataTemplate? - DataTemplate within a DataTemplateSelector referencing the parent DataTemplateSelector? DataTemplateSelector返回后,Datatemplate不会显示在ListView中 - Datatemplate not showing in ListView after being returned by DataTemplateSelector 如何在数据模板中获取文本? - How to get the text with in the datatemplate? 如何使此DataTemplateSelector工作? - How to make this DataTemplateSelector work? 使用DataTemplateSelector绑定到DataTemplate中枚举描述的多语言ComboBox - Multilingual ComboBox Bound to Enum Descriptions In DataTemplate Using DataTemplateSelector 当从DataTemplateSelector显式返回DataTemplate时,为什么DataTemplate不能绑定到接口? - Why can't a DataTemplate bind to an interface when that DataTemplate was explicitly returned from a DataTemplateSelector? 如何将自动生成的内容DataTemplate彼此对齐? - How to align auto generated content DataTemplate next to each other? 如何使网格在DataTemplateSelector中可见 - How make a grid visible in a DataTemplateSelector 如何在TreeViewItem的模板上使用DataTemplateSelector? - How to use DataTemplateSelector on TreeViewItem's template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM