繁体   English   中英

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

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

我正在编写一个聊天客户端,我的消息显示在列表框中。 在 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>

我想通过绑定控制我的网格属性的“Horizo​​ntalAlignment”,以便消息向右或向左对齐,具体取决于消息是发送还是接收。 我没有找到在互联网上做到这一点的方法,即使我知道它是可能的。 我只是还不了解绑定。

我的 C# 代码如下所示:

list_chat.Items.Add(textRange.Text);

提前致谢!

Horizo​​ntalAlignment 属性采用 System.Windows.Horizo​​ntalAlignment 类型。 您想绑定到该类型的属性。

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

然后你就可以像往常一样设置绑定。 如果您需要更新此值,请确保实现 INotifyPropertyChanged。

对于绑定(以最简单的形式):

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

替代:

如果您不希望 'Horizo​​ntalAlignmentValue' 属性处理其自己的逻辑,那么您可以使用 INotifyPropertyChanged 设置该属性,并将其设置在可能可以确定该值的其他地方,然后控制在更改时更新 UI。

暂无
暂无

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

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