C# Wpf列表框右对齐
下面的代码没有将文本向右对齐。如果我不使用Window.Resources(我想更改所选项目的颜色),代码可以工作。你能帮我吗C# Wpf列表框右对齐,c#,wpf,C#,Wpf,下面的代码没有将文本向右对齐。如果我不使用Window.Resources(我想更改所选项目的颜色),代码可以工作。你能帮我吗 <Window.Resources> <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem"> <Setter Property="Template"
<Window.Resources>
<Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="_Border"
Padding="2"
SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="_Border" Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="Yellow"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ListBox x:Name="myListBox" ItemContainerStyle="{DynamicResource _ListBoxItemStyle}" Margin="0,20,0,30"
VerticalAlignment="Top" Width="800" HorizontalAlignment="Right" MinHeight="200" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionChanged="myListBox_SelectionChanged" MouseDoubleClick="myListBox_MouseDoubleClick" Panel.ZIndex="2"
BorderThickness="0" FontSize="15" Background="{x:Null}"
HorizontalContentAlignment="Right">
</ListBox>
尝试设置项目的样式属性,否则您创建的资源窗口将为您设置样式属性 通过这样做,我能够实现您的愿望(可能有点棘手):
尝试设置项目的样式属性,否则您创建的资源窗口将为您设置样式属性 通过这样做,我能够实现您的愿望(可能有点棘手):
您正在替换
ListBoxItem
ControlTemplate,而您的模板忽略了HorizontalContentAlignment
属性:
修正:
您正在替换
ListBoxItem
ControlTemplate,而您的模板忽略了HorizontalContentAlignment
属性:
修正:
<ListBox x:Name="myListBox" ItemContainerStyle="{DynamicResource _ListBoxItemStyle}" Margin="0,20,0,30"
VerticalAlignment="Top" Width="800" HorizontalAlignment="Right" MinHeight="200" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Panel.ZIndex="2"
BorderThickness="0" FontSize="15" Background="{x:Null}"
HorizontalContentAlignment="Right">
<ListBoxItem>
<TextBlock HorizontalAlignment="Right" Text="Test"></TextBlock>
</ListBoxItem>
</ListBox>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="_Border"
Padding="2"
SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Border>