文本块样式绑定

发布于 2024-12-18 04:04:14 字数 1690 浏览 1 评论 0原文

我想使用 Viewmodel 绑定更改代码中的文本块颜色或样式。 这就是我得到的:

<TextBlock 
     Visibility="{Binding SubTitleVisibility}" 
     Text="{Binding SubTitle1}" 
     TextWrapping="Wrap" 
     Margin="12,-6,12,0" 
     Style="{Binding SubColor}"/>

除了样式之外,每个绑定都可以在这里工作。虽然它应用了字体大小但没有颜色。 (正如女王所说,这是一种魔法)

查看模型:

    internal DisplayMode Mode
    {
        get
        {
            return _mode;
        }
        set
        {
            //if (value != _type)
            //{
            _mode = value;
            if (value.Equals(DisplayMode.EditMode) && _type.Equals(ItemType.WithIcon))
            {
                _iconVisibility = Visibility.Visible;
                SubTitleVisibility = Visibility.Collapsed;
            }
            else if (_type.Equals(ItemType.WithSubTitle))
            {
                _iconVisibility = Visibility.Collapsed;
                SubTitleVisibility = Visibility.Visible;
            }

            NotifyPropertyChanged("Mode");
            NotifyPropertyChanged("SubColor");
        }
    }

    internal Style SubColor
    {
        get
        {
            Style temp; 
            if (_group != "phone" && _group != "email" && _mode == DisplayMode.ViewMode)
                temp = (Style)App.Current.Resources["PhoneTextNormalStyle"];
            else
                temp = (Style)App.Current.Resources["PhoneTextAccentStyle"];

            return temp;
        }
    }

我找到了这个codenippet 这里,但它甚至不会进入的 get 方法子颜色。

预先感谢您的帮助。

I want to change a textblock color or style in code with a Viewmodel binding.
This is what I got:

<TextBlock 
     Visibility="{Binding SubTitleVisibility}" 
     Text="{Binding SubTitle1}" 
     TextWrapping="Wrap" 
     Margin="12,-6,12,0" 
     Style="{Binding SubColor}"/>

Every binding works here except for the style. Though it applies the font size but without a color. (As Queen would say it's a kind of magic)

View model:

    internal DisplayMode Mode
    {
        get
        {
            return _mode;
        }
        set
        {
            //if (value != _type)
            //{
            _mode = value;
            if (value.Equals(DisplayMode.EditMode) && _type.Equals(ItemType.WithIcon))
            {
                _iconVisibility = Visibility.Visible;
                SubTitleVisibility = Visibility.Collapsed;
            }
            else if (_type.Equals(ItemType.WithSubTitle))
            {
                _iconVisibility = Visibility.Collapsed;
                SubTitleVisibility = Visibility.Visible;
            }

            NotifyPropertyChanged("Mode");
            NotifyPropertyChanged("SubColor");
        }
    }

    internal Style SubColor
    {
        get
        {
            Style temp; 
            if (_group != "phone" && _group != "email" && _mode == DisplayMode.ViewMode)
                temp = (Style)App.Current.Resources["PhoneTextNormalStyle"];
            else
                temp = (Style)App.Current.Resources["PhoneTextAccentStyle"];

            return temp;
        }
    }

I found this codesnippet here, but it won't even go into the get method of the SubColor.

Thanks in advance for your help.

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

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

发布评论

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

评论(1

活泼老夫 2024-12-25 04:04:14

你的做法是错误的。您不应尝试将样式绑定到 ViewModel,而应使用 数据模板选择器

Your approach is wrong. You shouldn't attempt to bind a style to your ViewModel, but instead use a Data Template Selector.

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