Skip to content

Instantly share code, notes, and snippets.

@danwalmsley
Last active October 26, 2022 19:08
Show Gist options
  • Save danwalmsley/8ba4517f87559d9739ead9c2a54e12c0 to your computer and use it in GitHub Desktop.
Save danwalmsley/8ba4517f87559d9739ead9c2a54e12c0 to your computer and use it in GitHub Desktop.
Colours
<UserControl xmlns="https://github.com/avaloniaui">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Canvas Width="120" Height="120">
<Rectangle Name="One" Height="44" Width="44" />
<Rectangle Name="Two" Height="44" Width="44" Canvas.Left="20" Canvas.Top="20" />
<Rectangle Name="Three" Height="44" Width="44" Canvas.Left="40" Canvas.Top="40" />
<Rectangle Name="Four" Height="44" Width="44" Canvas.Left="60" Canvas.Top="60" />
</Canvas>
<Button Name="Button" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center">
Click Me
</Button>
</StackPanel>
</UserControl>
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Controls.Shapes;
using Avalonia.Media;
namespace XamlPlayground.Views
{
public class SampleView : UserControl
{
private Rectangle _one;
private Rectangle _two;
private Rectangle _three;
private Rectangle _four;
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
_one = this.Find<Rectangle>("One");
_two = this.Find<Rectangle>("Two");
_three = this.Find<Rectangle>("Three");
_four = this.Find<Rectangle>("Four");
_one.Fill = Brushes.Blue;
_two.Fill = Brushes.Green;
_three.Fill = Brushes.Orange;
_four.Fill = Brushes.Red;
var button = this.Find<Button>("Button");
button.Click += (sender, e) =>
{
var one = _one.Fill;
var two = _two.Fill;
var three = _three.Fill;
var four = _four.Fill;
_one.Fill = four;
_two.Fill = one;
_three.Fill = two;
_four.Fill = three;
};
base.OnAttachedToVisualTree(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment