2

実際には、設計時にリソースから取得した画像を次のように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>

そして正常に動作します。しかし、(ログインボタンです)ユーザーがログインすると、ボタンの画像(長方形の内側)が変更されることを望みます..

どのようにできるのか?

4

1 に答える 1

3

DataTriggerモデルのプロパティが更新されたときに、を使用して画像を変更できます。

この例では、ブール値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; ここに画像の説明を入力してください IsLoggedIn = true; ここに画像の説明を入力してください

于 2013-02-11T21:24:27.327 に答える