/ / Як отримати Textblock з TextTrimming над AdornedElementPlaceholder? - wpf, прикраса, текстові обмеження

Як отримати Textblock за допомогою TextTrimming через AdornedElementPlaceholder? - wpf, adorner, обробка тексту

Я намагаюся отримати ValidationRule для відображенняНадішліть текст на скриньку, яка порушує правопорушення, якщо користувач ще не вказав значення. Я можу зробити так, щоб він відображався, але я не можу зробити так, щоб текст підходив до розміру комбобоксу за допомогою TextTrimming = "CharacterEllipsis". Як я можу змусити TextBlock підходити до комбінації, а також виправити себе, якщо користувач розмір вікна?

Ось мій 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);
}
}

Будь-яка допомога буде дуже вдячна! Дякую, Там

Відповіді:

1 для відповіді № 1

Спробуйте нижче, 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>