Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill xaml rectangle multiples solidcolorbrush

This type of fill in rectangle in xaml is it possible?

enter image description here

I don't want to use gradient to fill the rectangle, as in the image per section using solid colorbrush different.

Thanks

like image 234
Lu1zZz Avatar asked Feb 17 '26 23:02

Lu1zZz


2 Answers

Try this:

<Rectangle Width="300" Height="100" Stroke="black" StrokeThickness="3">
  <Rectangle.Fill>
    <LinearGradientBrush StartPoint="0 0" EndPoint="1 0">
      <GradientStop Color="red" Offset="0"/>
      <GradientStop Color="red" Offset=".33"/>
      <GradientStop Color="black" Offset=".33"/>
      <GradientStop Color="black" Offset=".34"/>
      <GradientStop Color="green" Offset=".34"/>
      <GradientStop Color="green" Offset=".66"/>
      <GradientStop Color="black" Offset=".66"/>
      <GradientStop Color="black" Offset=".67"/>
      <GradientStop Color="cyan" Offset=".67"/>
    </LinearGradientBrush>
  </Rectangle.Fill>
</Rectangle>
like image 85
Hamlet Hakobyan Avatar answered Feb 19 '26 12:02

Hamlet Hakobyan


Re: your comment -- is the concern simply that it's an N amount of rectangles? If it's not a constraint that it all be one rectangle, you could do something like this:

    <ListView ItemsSource="{Binding ColorsTiles}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ListView ItemsSource="{Binding}">
                    <ListView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ListView.ItemsPanel>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Rectangle Fill="{Binding}" Width="100" Height="100" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

and

ColorsTiles = new ObservableCollection<ObservableCollection<SolidColorBrush>>() {
        new ObservableCollection<SolidColorBrush>(){ new SolidColorBrush(Colors.LightGreen), new SolidColorBrush(Colors.LightBlue), new SolidColorBrush(Colors.Blue) },
        new ObservableCollection<SolidColorBrush>(){ new SolidColorBrush(Colors.LightSeaGreen), new SolidColorBrush(Colors.Pink), new SolidColorBrush(Colors.Red) } };
like image 42
bdimag Avatar answered Feb 19 '26 11:02

bdimag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!