在设置的VisualBrush运行时图像用C# [英] Set image runtime in VisualBrush with c#
问题描述
其实我设置为从资源的图像了在设计时进 XAML 文件是这样的:
actually I set an image took from the resources at designtime into the xaml file like this:
<Button Click="btnLogin_Click" Name="btnLogin">
<StackPanel Orientation="Horizontal">
<Rectangle Width="20" Height="20" Name="recLogin">
<Rectangle.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="White" />
</Rectangle.Resources>
<Rectangle.Fill>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_user}" x:Name="brushLogin" />
</Rectangle.Fill>
</Rectangle>
<TextBlock Text=" login" Name="txbLogin" />
</StackPanel>
</Button>
和正常工作。但是的(是登录按钮)的我想,当用户执行登录时,图像上的按钮的(矩形内)的将被改变。
and works fine. But (is a login button) I wish that when the user do a login, the image on the button (inside a rectangle) will be changed..
我该怎么办?
推荐答案
您可以使用 DataTrigger
来改变图像时,属性更新在你的模型。
You can use a DataTrigger
to change the image when a property updates in your model.
在这个例子中,布尔值 IsLoggedIn
被改变从而改变图像
In this example the boolean value IsLoggedIn
is changed which in turn changes the image.
例如:
XAML中:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="125.078" Width="236.441" Name="UI" >
<Window.Resources>
<VisualBrush x:Key="Loggedin">
<VisualBrush.Visual>
<Image Source="http://icons.iconarchive.com/icons/deleket/sleek-xp-basic/32/Ok-icon.png" Stretch="Uniform" />
</VisualBrush.Visual>
</VisualBrush>
<VisualBrush x:Key="NotLoggedin">
<VisualBrush.Visual>
<Image Source="http://icons.iconarchive.com/icons/deleket/sleek-xp-basic/32/Close-2-icon.png" Stretch="Uniform" />
</VisualBrush.Visual>
</VisualBrush>
</Window.Resources>
<Grid DataContext="{Binding ElementName=UI}">
<Button Click="btnLogin_Click" Name="btnLogin" HorizontalAlignment="Left" Width="94" Height="40" VerticalAlignment="Top" Margin="63,26,0,0">
<StackPanel Orientation="Horizontal">
<Rectangle Width="20" Height="20" Name="recLogin">
<Rectangle.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="White" />
</Rectangle.Resources>
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Fill" Value="{StaticResource NotLoggedin}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsLoggedIn}" Value="True">
<Setter Property="Fill" Value="{StaticResource Loggedin}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
<TextBlock Text=" login" Name="txbLogin" />
</StackPanel>
</Button>
</Grid>
</Window>
代码:
Code:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool _isLoggedIn;
public MainWindow()
{
InitializeComponent();
}
public bool IsLoggedIn
{
get { return _isLoggedIn; }
set { _isLoggedIn = value; NotifyPropertyChanged("IsLoggedIn"); }
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
IsLoggedIn = !IsLoggedIn;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
注意:我只是用网上图像,我没有你的ressources,你可以改变,以满足您的需求。
Note: I just used online images as I dont have your ressources, you can change to suit your needs
结果:
IsLoggedIn = FALSE;
IsLoggedIn = TRUE;
IsLoggedIn = false; IsLoggedIn = true;
这篇关于在设置的VisualBrush运行时图像用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!