c# - 使用 C# 在 VisualBrush 中设置图像运行时

标签 c# wpf image xaml visualbrush

实际上,我将设计时从资源中获取的图像设置到 xaml 文件中,如下所示:

<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>

并且工作正常。但是(是一个登录按钮)我希望当用户登录时,按钮上的图像(矩形内)将会改变..

我该怎么办?

最佳答案

当模型中的属性更新时,您可以使用 DataTrigger 更改图像。

在此示例中, bool 值 IsLoggedIn 发生更改,进而更改图像。

示例:

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>

代码:

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));
        }
    }
 }

注意:我只是使用在线图片,因为我没有您的资源,您可以更改以满足您的需求

结果:

IsLoggedIn = false; enter image description here 已登录 = true; enter image description here

关于c# - 使用 C# 在 VisualBrush 中设置图像运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14819872/

相关文章:

c# - 我为什么要购买 ReSharper 或 CodeRush?

c# - 如何在 Windows 版 MonoGame 中制作屏幕截图?

c# - 将 UserControl 命令绑定(bind)到 MainWindow WPF/MVVM

wpf - 如何防止 InvokeCommandAction 将事件传播到父元素?

excel - 如何用VBA在Excel指定单元格位置插入图片

c# - UWP 以编程方式更改 XAML 页面的背景图像 c#

image - 从屏幕截图保存图像时图像质量不佳( flutter )

c# - 如何从自动缩放图表控件中获取最大 Y 轴值

c# - 在 MVP-VM 事件或按钮单击中使用 WPF xaml

c# - Azure Blob 存储的文件大小限制?