简体   繁体   English

如何在 WPF 中制作条件扩展器

[英]How to make conditional Expander in WPF

I have a very simple task.我有一个非常简单的任务。

I have already created a custom expander so it's working fine.我已经创建了一个自定义扩展器,所以它工作正常。

I just stuck at making it conditional我只是坚持让它有条件

if my text content is More I want to show expander else I don't want to.如果我的文本内容更多,我想显示扩展器,否则我不想显示。

Here is an image of it这是它的图像

我想实现这一目标

Sounds like you may need some custom converters .听起来您可能需要一些自定义转换器 You could do something like this:你可以这样做:

public class LengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value?.GetType() == typeof(string))
        {
            string text = (string)value;
            return text.Length > 20 ? Visibility.Visible : Visibility.Collapsed;
        }
        return Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then use it in xaml like this:然后像这样在xaml使用它:

<Window.Resources>
    <local:LengthConverter x:Key="LengthConverter" />
</Window.Resources>

...

<Expander Visibility="{Binding SomeText, Converter={StaticResource LengthConverter}}">
    <TextBlock Text="{Binding SomeText}" />
</Expander>

This is not a full solution.这不是一个完整的解决方案。 I don't know what your expander looks like, and you will want to make it responsive instead of hardcoding the length of the string.我不知道你的扩展器是什么样的,你会想要让它响应而不是硬编码字符串的长度。 But this may put you on the right path.但这可能会让你走上正确的道路。

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

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