How do you color a rectangle in C# that has been declared in XAML in WPF?

后端 未结 8 2099
长情又很酷
长情又很酷 2020-12-20 11:26

How do you color a rectangle in C# that has been declared in XAML in WPF?

There is a rectangle control in XAML. In my C# code there are times in which it would be n

相关标签:
8条回答
  • 2020-12-20 11:55

    Wpf binding would be able to do this without having to reference the control by name in code:

    <Rectangle Width="480" Height="200" Fill="{Binding Path=FillColor}"/>
    

    Then put a property on your DataContext class, assuming you have implemented INotifyPropertyChanged:

    public Brush FillColor  
    {   
      get { return this.fillColor; }   
      set
      {
         this.fillColor = value;
         if (PropertyChanged != null)
         {
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }   
      }
    }
    

    Then you can assign your required color to the FillColor property and the UI will update itself.

    0 讨论(0)
  • 2020-12-20 11:55

    For all of the people searching for this in 2019, I'm using WPF and you just add this property to your rectangle:

    Your Rectangle should look something like this:

        <Rectangle HorizontalAlignment="Left" Height="75" Stroke="Black" StrokeThickness="4" VerticalAlignment="Top" Width="792"/>
    

    Right before the forward slash, add in Fill="YourColorHere", for example, I'm using this: Fill="AliceBlue". IntelliSense should give you a list of colors to scroll through.

    0 讨论(0)
提交回复
热议问题