2013-02-11 59 views
2

其實我設置爲從資源的圖像了在設計時爲XAML文件是這樣的:設置圖像的運行時間與C#

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

和工作正常。但是(是登錄按鈕)我希望當用戶做一次登錄時,圖像上的按鈕(矩形內)將被改變..

我該怎麼辦?

回答

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

注:我只是用網上圖片爲我沒有你的ressources,你可以改變,以滿足您需要

結果:

IsLoggedIn = false; enter image description here IsLoggedIn = true; enter image description here

+0

thankyou !!,我還有一個小問題:在我的xaml中,我使用了一個'ResourceDictionary' ...,它爲'>創建衝突,另一個... – ghiboz 2013-02-11 22:29:35

+0

ResourceDictionary是否包含具有相同密鑰的資源?您應該能夠使用任何你想要的名字, – 2013-02-11 22:35:22

+0

這裏是我的資源:' ' – ghiboz 2013-02-11 22:38:36