![](/img/trans.png)
[英]How to prevent a TextBlock with TextTrimming CharacterEllipsis to extend Column with Width Auto
[英]How to get Textblock with TextTrimming over an AdornedElementPlaceholder?
如果用戶尚未指定值,我正在嘗試獲取 ValidationRule 以在有問題的 combobox 上顯示文本。 我可以讓它顯示,但我似乎無法使用 TextTrimming="CharacterEllipsis" 使文本適合 combobox 的大小。 如果用戶調整 window 的大小,我怎樣才能讓 TextBlock 適合 combobox,並自行糾正?
這是我的 MainWindow.xaml:
<Window x:Class="PocAdornedElementPlaceholder.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PocAdornedElementPlaceholder"
Title="MainWindow" Height="200" Width="150">
<Window.Resources>
<ControlTemplate x:Key="ValidationTemplate">
<Grid HorizontalAlignment="Center">
<AdornedElementPlaceholder/>
<TextBlock Foreground="Red"
TextTrimming="CharacterEllipsis"
Text="{Binding ErrorContent}"
IsHitTestVisible="False"
VerticalAlignment="Center"
Margin="5,0,0,0"/>
</Grid>
</ControlTemplate>
</Window.Resources>
<Grid>
<ComboBox Margin="10"
Validation.ErrorTemplate="{StaticResource ValidationTemplate}"
VerticalAlignment="Center"
ItemsSource="{Binding Options}">
<ComboBox.Text>
<Binding Path="SelectedValue">
<Binding.ValidationRules>
<local:MyValidationRule ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</ComboBox.Text>
</ComboBox>
</Grid>
</Window>
這是我的 MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Options = new List<string>() { "Value 1", "Value 2", "Value 3", "" };
this.DataContext = this;
}
public string SelectedValue { get; set; }
public List<string> Options { get; set; }
}
這是我的 MyValidationRule.cs 文件:
public class MyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (string.IsNullOrEmpty((string)value))
return new ValidationResult(false, "Value cannot be empty!");
return new ValidationResult(true, null);
}
}
任何幫助將不勝感激,謝謝,譚
試試下面,TextBlock 應該是裝飾器的內容。 我還必須更改文本塊的邊距以計算下拉箭頭按鈕。
<ControlTemplate x:Key="ValidationTemplate">
<Grid HorizontalAlignment="Center">
<AdornedElementPlaceholder>
<TextBlock Foreground="Red" TextTrimming="CharacterEllipsis" Text="{Binding ErrorContent}" IsHitTestVisible="False" VerticalAlignment="Center" Margin="5,0,20,0" />
</AdornedElementPlaceholder>
</Grid>
</ControlTemplate>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.