Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wpf列表框右对齐_C#_Wpf - Fatal编程技术网

C# Wpf列表框右对齐

C# Wpf列表框右对齐,c#,wpf,C#,Wpf,下面的代码没有将文本向右对齐。如果我不使用Window.Resources(我想更改所选项目的颜色),代码可以工作。你能帮我吗 <Window.Resources> <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem"> <Setter Property="Template"

下面的代码没有将文本向右对齐。如果我不使用Window.Resources(我想更改所选项目的颜色),代码可以工作。你能帮我吗

 <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>