简体   繁体   English

WPF:绑定到网格的“Horizo​​ntalAlignment”属性

[英]WPF: Binding to “HorizontalAlignment” Property of Grid

I am writing a chatclient and my messages get displayed in a listbox.我正在编写一个聊天客户端,我的消息显示在列表框中。 In the xaml my listbox is set up like this:在 xaml 中,我的列表框设置如下:

<ListBox x:Name="list_chat" Background="{x:Null}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="256,0,0,64" BorderThickness="0" BorderBrush="{x:Null}" Foreground="White" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="False" Focusable="False" Grid.ColumnSpan="2">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid Background="#bd93f9" Margin="64,0,8,0" HorizontalAlignment="Right">
                                    <TextBlock Text="{Binding}" TextWrapping="Wrap" LineStackingStrategy="MaxHeight" Foreground="White" HorizontalAlignment="Right" VerticalAlignment="Stretch" Margin="16,8,32,0" LineHeight="Auto" TextTrimming="None" TextAlignment="Right" Width="Auto" Padding="0" UseLayoutRounding="True">
                                    </TextBlock>
                                    <Button HorizontalAlignment="Right" VerticalAlignment="Center" Width="32" Height="32" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="White" Margin="0">
                                        <Button.Style>
                                            <Style TargetType="{x:Type Button}">
                                                <Setter Property="Template">
                                                    <Setter.Value>
                                                        <ControlTemplate TargetType="Button">
                                                            <Border
                                    x:Name="border"
                                    Background="{x:Null}"
                                    BorderBrush="{x:Null}"
                                    BorderThickness="0"
                                    CornerRadius="90"
                                    TextBlock.Foreground="White">
                                                                <Grid>
                                                                    <Image
                                            x:Name="buttonImage"
                                            Source="C:\Users\janke\source\repos\Unichat\Unichat\bin\Debug\pictures\icons\reply-line.png" Width="16" Height="16"
                                            />
                                                                    <ContentPresenter
                                            Margin="{TemplateBinding Padding}"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center" />
                                                                </Grid>
                                                            </Border>
                                                            <ControlTemplate.Triggers>
                                                                <Trigger Property="IsMouseOver" Value="true">
                                                                    <Setter TargetName="border" Property="Background" Value="{x:Null}" />
                                                                    <Setter TargetName="border" Property="BorderBrush" Value="{x:Null}" />
                                                                    <Setter TargetName="buttonImage" Property="Source" Value="C:\Users\janke\source\repos\Unichat\Unichat\bin\Debug\pictures\icons\reply-fill.png" />
                                                                </Trigger>
                                                                <Trigger Property="IsPressed" Value="true">
                                                                    <Setter TargetName="border" Property="Background" Value="{x:Null}" />
                                                                    <Setter TargetName="border" Property="BorderBrush" Value="{x:Null}" />
                                                                </Trigger>
                                                            </ControlTemplate.Triggers>
                                                        </ControlTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                            </Style>
                                        </Button.Style>
                                    </Button>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

I want to control the "HorizontalAlignment" of my grid property with a binding so the message either gets aligned right or left, depending on if the message is sent or recieved.我想通过绑定控制我的网格属性的“Horizo​​ntalAlignment”,以便消息向右或向左对齐,具体取决于消息是发送还是接收。 I didn't find a way to do that on the internet, even though I know its possible.我没有找到在互联网上做到这一点的方法,即使我知道它是可能的。 I just don't understand bindings just yet.我只是还不了解绑定。

My C# code looks like this:我的 C# 代码如下所示:

list_chat.Items.Add(textRange.Text);

Thanks in advance!提前致谢!

The HorizontalAlignment property takes a type of System.Windows.HorizontalAlignment. Horizo​​ntalAlignment 属性采用 System.Windows.Horizo​​ntalAlignment 类型。 You want to bind to a property of that type.您想绑定到该类型的属性。

public HorizontalAlignment HorizontalAlignmentValue 
{
   get
   {
      if (--whatever--)
          return HorizontalAlignment.Left;
      else if (--whatever--)
          return HorizontalAlignment.Right;
   }
}

Then you would just set up the binding like usual.然后你就可以像往常一样设置绑定。 If you will need to update this value then be sure to implement INotifyPropertyChanged.如果您需要更新此值,请确保实现 INotifyPropertyChanged。

For the binding (in it simplest form):对于绑定(以最简单的形式):

<Grid HorizontalAlignment="{Binding HorizontalAlignmentValue}" />

Alt:替代:

If you don't want the 'HorizontalAlignmentValue' property to handle its own logic, then you could set up the property with INotifyPropertyChanged and set it elsewhere presumably where the value can be determined, then control updating the UI on change.如果您不希望 'Horizo​​ntalAlignmentValue' 属性处理其自己的逻辑,那么您可以使用 INotifyPropertyChanged 设置该属性,并将其设置在可能可以确定该值的其他地方,然后控制在更改时更新 UI。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM