WPF:文本块的条件模板

发布于 2024-09-08 08:39:41 字数 367 浏览 4 评论 0原文

我在 itemscontrol 中有一堆文本块...我需要知道如何根据文本在数据模型的列表中是否可用来为文本块中的文本添加下划线。

对我来说听起来很简单...但是自过去 8 小时以来,我一直在谷歌搜索...

我可以使用 datatriggers 和 valueconverters 来实现此目的吗?如果是,那么我如何执行 viewModel 中的方法(该方法帮助我检查数据模型列表中是否存在给定的文本)...

即使我使用条件模板......如何我是否访问模型中的列表(视图模型可以获取它...但是我如何访问视图模型?)..

这应该是一件相当容易做的事情...我真的错过了一些非常简单的东西吗这里?? :)

我的应用程序遵循 MVVM 模式。

I have a bunch of textblocks in an itemscontrol... I need to know how can I underline the text in the textblock based on whether the text is available in a list in the data model..

Sounds very simple to me...but I have been googling since the past 8 hrs...

Can I use datatriggers and valueconverters for this purpose? If yes, then how can I execute the method which lies in the viewModel (the method which helps me to check whther a given a text exists in the data model list)...

Even if I go for conditional templating....how do I access the list which lies in my model (the viewmodel can fetch it...but then how do i access the viewmodel?)..

This should be a fairly easy thing to do...Am I really missing something very simple here?? :)

I am following the MVVM pattern for my application..

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

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

发布评论

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

评论(1

巴黎盛开的樱花 2024-09-15 08:39:41

一种方法是使用多值转换器,它是一个实现 IMultiValueConverter。多值转换器允许您绑定到多个值,这意味着您可以在值转换器中获取对视图模型和 TextBlock 文本的引用。

假设您的视图模型有一个名为 GetIsUnderlined 的方法,该方法返回 true 或 false 指示文本是否应加下划线,您的 valueconverter 可以按照以下方式实现:

class UnderlineValueConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var viewmodel = values[0] as Window1ViewModel;
        var text = values[1] as string;
        return viewmodel.GetIsUnderlined(text) ? TextDecorations.Underline : null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter,     System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

您可以通过以下方式使用此 valueconverter 文本块

<Grid x:Name="grid1" >
    <Grid.Resources>
        <local:UnderlineValueConverter x:Key="underlineValueConverter" />
    </Grid.Resources>

    <TextBlock Text="Blahblah">
        <TextBlock.TextDecorations>
            <MultiBinding Converter="{StaticResource underlineValueConverter}">
                <Binding /> <!-- Pass in the DataContext (the viewmodel) as the first parameter -->
                <Binding Path="Text" RelativeSource="{RelativeSource Mode=Self}" /> <!-- Pass in the text of the TextBlock as the second parameter -->
            </MultiBinding>
        </TextBlock.TextDecorations>
    </TextBlock>
</Grid>

One way is to use a multivalueconverter which is a class that implements IMultiValueConverter. A multivalueconverter allows you to bind to several values which means that you can get a reference to both your viewmodel and the text of your TextBlock in your valueconverter.

Assuming that your viewmodel has a method called GetIsUnderlined that returns true or false indicating whether or not the text should be underlined your valueconverter can be implemented along these lines:

class UnderlineValueConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var viewmodel = values[0] as Window1ViewModel;
        var text = values[1] as string;
        return viewmodel.GetIsUnderlined(text) ? TextDecorations.Underline : null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter,     System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

You can use this valueconverter in the following way for a TextBlock:

<Grid x:Name="grid1" >
    <Grid.Resources>
        <local:UnderlineValueConverter x:Key="underlineValueConverter" />
    </Grid.Resources>

    <TextBlock Text="Blahblah">
        <TextBlock.TextDecorations>
            <MultiBinding Converter="{StaticResource underlineValueConverter}">
                <Binding /> <!-- Pass in the DataContext (the viewmodel) as the first parameter -->
                <Binding Path="Text" RelativeSource="{RelativeSource Mode=Self}" /> <!-- Pass in the text of the TextBlock as the second parameter -->
            </MultiBinding>
        </TextBlock.TextDecorations>
    </TextBlock>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文