Reputation: 93
Changing the content image of a button makes it ignore any previously defined layout properties.
What I suspect is the fact that upon changing this.Content
in the Button click event, it modifies:
<Button>
Everything found between these tags.
</Button>
And as my Style is inside those tags, it overrides it.
Here is the code for record:
private void ChangeImageFile(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files|*.png;*.jpeg;*.tiff;*.gif;*.bmp;*.jpg";
ofd.FilterIndex = 1;
ofd.Multiselect = false;
bool? ok = ofd.ShowDialog();
if(ok == true)
{
Image loaded = new Image();
loaded.Source = new BitmapImage(new Uri(ofd.FileName));
loaded.Height = 100;
this.Content = loaded;
}
}
<Button x:Name="BookCover" Click="ChangeImageFile">
<Button.Content>
<Image Source="Images/NewBook.png"/>
</Button.Content>
<Button.Style>
<Style TargetType="{x:Type Button}">
... LONG STYLE ...
</Style>
</Button.Style>
</Button>
Upvotes: 0
Views: 42
Reputation: 30022
When you're using this.Content
, this refers to the current window and not to the button. You either need to access the button by Name or cast the sender.
By Name:
BookCover.Content = loaded;
Cast the sender:
Button btn = (sender as Button);
if(btn != null)
btn.Content = loaded;
Upvotes: 1