[英]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>
我想通過綁定控制我的網格屬性的“HorizontalAlignment”,以便消息向右或向左對齊,具體取決於消息是發送還是接收。 我沒有找到在互聯網上做到這一點的方法,即使我知道它是可能的。 我只是還不了解綁定。
我的 C# 代碼如下所示:
list_chat.Items.Add(textRange.Text);
提前致謝!
HorizontalAlignment 屬性采用 System.Windows.HorizontalAlignment 類型。 您想綁定到該類型的屬性。
public HorizontalAlignment HorizontalAlignmentValue
{
get
{
if (--whatever--)
return HorizontalAlignment.Left;
else if (--whatever--)
return HorizontalAlignment.Right;
}
}
然后你就可以像往常一樣設置綁定。 如果您需要更新此值,請確保實現 INotifyPropertyChanged。
對於綁定(以最簡單的形式):
<Grid HorizontalAlignment="{Binding HorizontalAlignmentValue}" />
替代:
如果您不希望 'HorizontalAlignmentValue' 屬性處理其自己的邏輯,那么您可以使用 INotifyPropertyChanged 設置該屬性,並將其設置在可能可以確定該值的其他地方,然后控制在更改時更新 UI。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.