如何通过 AdornedElementPlaceholder 使用 TextTrimming 获取文本块?

发布于 2024-11-28 20:03:05 字数 2577 浏览 6 评论 0原文

如果用户尚未指定值,我正在尝试获取 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);
    }
}

任何帮助将不胜感激! 谢谢, 谭

I'm trying to get a ValidationRule to display text over an offending combobox, if the user has not specified a value yet. I can get it to display, but I can't seem to get the text to fit to the size of the combobox using TextTrimming="CharacterEllipsis". How can I get the TextBlock to fit the combobox, and also correct itself if the user resizes the window?

Here's my 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>

Here's my 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; }
}

And here's my MyValidationRule.cs file:

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);
    }
}

Any help would be much appreciated!
Thanks,
Tam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我要还你自由 2024-12-05 20:03:05

尝试下面,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>

Try below, TextBlock should be the content of the adorner. I also had to change the margin of the textblock to count for the dropdown arrow button.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文