Reputation: 41
So I have a seemingly simple question:
How do I go about coding an "if rectangle.fill is X, change fill to this specific image" in C#? Here is my rectangle in XAML:
<Rectangle Height="auto" Width="auto" x:Name="Honey" ManipulationStarted="Honey_Started">
<Rectangle.Fill>
<ImageBrush ImageSource="image100.jpg" />
</Rectangle.Fill>
</Rectangle>
That was simple, but how would the code behind look? The following example is obviously wrong, but am just including it to hopefully show what I wish to accomplish.
private void Honey_Started(object sender, ManipulationStartedEventArgs e)
{
if (Honey.Fill == image100.jpg)
{
Honey.Fill = 900image.jpg;
}
}
Upvotes: 4
Views: 1951
Reputation: 69372
Instead of checking the filename of the Fill
, you could change the Rectangle's Tag
property (or create your own UserControl
). Then, for example, you could do this:
<Rectangle Height="auto" Width="auto" x:Name="Honey" ManipulationStarted="Honey_Started" Tag="image1">
<Rectangle.Fill>
<ImageBrush ImageSource="image100.jpg" />
</Rectangle.Fill>
</Rectangle>
I've added Tag="image1"
as an attribute in your Rectangle
. You can then access this in the codebehind and check.
private void Honey_Started(object sender, ManipulationStartedEventArgs e)
{
if (Honey.Tag.ToString() == "image1")
{
ImageBrush ib = new ImageBrush();
BitmapImage bImage = new BitmapImage(new Uri("900image.jpg",UriKind.Relative));
ib.ImageSource = bImage;
Honey.Tag = "900image.jpg";
Honey.Fill = ib;
}
}
There are other methods as well. All I've done in the Honey_Started
event is create a new ImageBrush
in code, assigned it an image (900image.jpg
) and then set that ImageBrush
to be the Fill
value for the rectangle.
An alternative method could be two store the two ImageBrushes
as a class level variable and use them to compare the value.
BitmapImage biImage100 = new BitmapImage(new Uri("/Jar/image100.jpg",UriKind.Relative));
ImageBrush ibImage100 = new ImageBrush() { ImageSource = biImage100 };
ImageBrush ib900image = new ImageBrush() { /*assign the 900image.jpg image*/ };
private void Honey_Started(object sender, ManipulationStartedEventArgs e)
{
if (Honey.Fill == ibImage100)
{
Honey.Fill = ib900image;
}
}
Upvotes: 3