wpf XAML 将 Rectange.Fill SolidColorBrush 绑定到 Color 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19445422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:53:30  来源:igfitidea点击:

XAML binding Rectange.Fill SolidColorBrush to Color property

c#wpfxamlmvvm

提问by Carl di Ortus

I'm trying to display a TextBox and a Rectangle both show the Color property in another class. I use Caliburn.Micro and MVVM approach (I'm new at this).

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

My problem is that the TextBox writes the Color.ToString by default, but the Rectangle does not fill with the same Color, in fact the rectangle is invisible all the time. How to fill it?

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

Extract from XAML View:

从 XAML 视图中提取:

<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>

Extract from C# ViewModel:

摘自 C# ViewModel:

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

Inside the DesignSettings class I have this property to be binded:

在 DesignSettings 类中,我要绑定此属性:

public Color EdgeHighlightOutOfRangeColor { get; set; }

采纳答案by Mukesh Rawat

Your code should work unless you are using wrong namespace for Color property.

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

Color can be found under System.Drawingand System.Windows.Media.Colors. Make sure you are using System.Windows.Media in order to bind it with Rectangle.

颜色可以在System.DrawingSystem.Windows.Media.Colors下找到。确保您正在使用 System.Windows.Media 以便将其与 Rectangle 绑定。

With System.Drawing, it will come like below; without filled rectangle

使用 System.Drawing,它将如下所示;没有填充矩形

enter image description here

在此处输入图片说明

回答by Sheridan

Sorry to tell you this, but your code works fine for me without making any changes:

很抱歉告诉您这一点,但您的代码对我来说很好用,无需进行任何更改:

enter image description here

在此处输入图片说明

UPDATE >>>

更新 >>>

If you resize your Windowto make it larger, you should see the Rectangleappear from behind the TextBlock. This is one excellent reason notto use the drag and drop functionality of the Visual Studio designer and Toolbox. If you use rows and columns in your Gridcorrectly, you won't get this problem.

如果你调整你Window使其变大,你应该看到Rectangle从后面出现TextBlock。这是使用 Visual Studio 设计器和工具箱的拖放功能的一个很好的理由。如果您Grid正确使用行和列,则不会遇到此问题。

回答by Nitin

It should work. The problem why you are not seeing your Rectangleis your are placing TextBoxand Rectanglein the same Grid.Rowand same Grid.Columnof Gridhence Textboxis overlapping on your Rectangle. You will have to place them in different Columns.

它应该工作。为什么你没有看到这个问题你Rectangle是你正在放置TextBoxRectangle在相同的Grid.Row和相同Grid.ColumnGrid,因此Textbox是在您的重叠Rectangle。您必须将它们放在不同的列中。