c# - XAML 将 Rectange.Fill SolidColorBrush 绑定(bind)到 Color 属性

标签 c# wpf xaml mvvm

我正在尝试显示一个 TextBox 和一个 Rectangle,它们都在另一个类中显示 Color 属性。我使用 Caliburn.Micro 和 MVVM 方法(我是新手)。

我的问题是 TextBox 默认写入 Color.ToString,但 Rectangle 不会填充相同的 Color,实际上矩形一直是不可见的。如何填写?

从 XAML View 中提取:

<TextBox Grid.Row="7" Grid.Column="1" Margin="10,7,20,7"
         Text="{Binding Path=Design.EdgeHighlightOutOfRangeColor}"
         FontSize="12" FontFamily="Rockwell" Width="110" HorizontalAlignment="Left"/>
<Rectangle Grid.Row="7" Grid.Column="1" Width="20" Height="20" Margin="100,7,20,7">
    <Rectangle.Fill>
        <SolidColorBrush Color="{Binding Path=Design.EdgeHighlightOutOfRangeColor}"/>
    </Rectangle.Fill>
</Rectangle>

从 C# ViewModel 中提取:

_designSettings = _settings.DesignSettings;
public DesignSettings Design
{
    get { return _designSettings; }
    set
    {
        _designSettings = value;
        NotifyOfPropertyChange(() => Design);
    }
}

在 DesignSettings 类中,我有这个属性要绑定(bind):

public Color EdgeHighlightOutOfRangeColor { get; set; }

最佳答案

除非您为 Color 属性使用了错误的命名空间,否则您的代码应该可以正常工作。

颜色可以在System.Drawing下找到和 System.Windows.Media.Colors .确保您使用的是 System.Windows.Media 以便将其与 Rectangle 绑定(bind)。

有了System.Drawing,它会像下面这样;无填充矩形

enter image description here

关于c# - XAML 将 Rectange.Fill SolidColorBrush 绑定(bind)到 Color 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19445422/

相关文章:

c# - 在位图中查找 Blob

c# - Winrt Xaml Grid ROw 没有用 * 填充空间

c# - 一种根据另一种风格激活的风格?

c# - 在 WPF 中查看 Word 文档

c# - 在 ScatterViewItem 中将控制区与图像分离

c# - 在 IIS 服务器上执行 javascript(服务器端)

c# - 为什么 NetMQ 不能在 NUnit 环境中工作

c# - listbox isSelected DataTemplate 中的数据绑定(bind)

wpf - 与 TextBlock 具有相同剪辑的 TextBox(TextBox 模板)

c# - 尝试制作缩放效果