2016-07-23 67 views
0
C#WPF路由事件

所以我編碼具有(現在)的應用程序,其中我分組一些其它元件的一個用戶控制。我想在用戶控件內的屬性更改時更新視圖。我試圖實現的inotify,它似乎工作(我已經做了同樣的方式在我的主要形式,有它的罰款)。還嘗試在XAML中設置綁定,但無法以某種方式通過它。我想我必須實現路由事件,但我不確定是否以及如何在這種情況下。從用戶控制的MainForm

我注意到那是什麼,我可以從OnPropertyChanged方法去除一些東西,但如果我想刪除目前未分配相應的事件,它指出我沒有實現的inotify。從用戶控制

XAML:

<UserControl x:Class="Tool_WPF.frmLogin" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:Fever_Tool_WPF" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 

<Grid> 
    <StackPanel> 
     <Label Name="labLogin" HorizontalAlignment="Center" MaxWidth="150"> 
      <Style> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Path=LoginState, ElementName=labLogin}" Value="1"> 
         <Setter Property="Label.Content" Value="Success"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </Label> 
     <Label HorizontalAlignment="Center">Forum username</Label> 
     <TextBox x:Name="tbUser" Width="150"></TextBox> 
     <Label HorizontalAlignment="Center">Forum password</Label> 
     <PasswordBox x:Name="tbPassword" Width="150"></PasswordBox> 
     <Button x:Name="cmdLogin" HorizontalAlignment="Center" Margin="10" Width="150" Click="cmdLogin_Click">Login</Button> 
    </StackPanel> 
</Grid> 

背後用於用戶控制代碼(屬性類)

 public class StateLogin : INotifyPropertyChanged 
    { 
     private int _loginState; // helping variable to trigger login state change 

     public event PropertyChangedEventHandler PropertyChanged; 

     public int LoginState 
     { 
      get { return _loginState; } 
      set 
      { 
       _loginState = value; 
       OnPropertyChanged("LoginState"); 
      } 
     } 

     protected void OnPropertyChanged(string name) 
     { 
      new PropertyChangedEventArgs(name); 
     } 
    } 

一些主窗口XAML代碼的

<RibbonWindow 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:Fever_Tool_WPF" 
    xmlns:Primitives="clr-namespace:System.Windows.Controls.Ribbon.Primitives;assembly=System.Windows.Controls.Ribbon" x:Class="Fever_Tool_WPF.MainWindow" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="589" Width="896" 
    Initialized="MainWindow_Initialized"> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition/> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Ribbon x:Name="ribbonMain"> 
     <Ribbon.HelpPaneContent> 
      <RibbonButton SmallImageSource="Icons/MainWindow/help.ico"/> 
     </Ribbon.HelpPaneContent> 
     <Ribbon.QuickAccessToolBar> 
      <RibbonQuickAccessToolBar/> 
     </Ribbon.QuickAccessToolBar> 
     <Ribbon.ApplicationMenu> 
      <RibbonApplicationMenu SmallImageSource="Icons/MainWindow/BlogHomePage.ico"> 
       <RibbonApplicationMenuItem Header="Information" ImageSource="Icons/MainWindow/Info.ico" Click="RibInfo_Click"/> 
      </RibbonApplicationMenu> 
     </Ribbon.ApplicationMenu> 
     <RibbonTab x:Name="rLogin" Header="Login" Visibility="Visible"/> 
     <RibbonTab x:Name="rMPL" Header="MPL"/> 
    </Ribbon> 


    <local:frmLogin x:Name="frmLogin" Margin="10, 10, 10, 10" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="1" Visibility="Visible"/> 


    <StatusBar DockPanel.Dock="Bottom" MinHeight="10" MaxHeight="20" Grid.Row="2"> 
     <StatusBarItem DockPanel.Dock="Right"> 
      <Image> 
       <Image.Style> 
        <Style> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding Ping}" Value="false"> 
           <Setter Property="Image.Source" Value="Icons/MainWindow/StatusOffline_stop_32x.png"/> 
           <Setter Property="Image.ToolTip" Value="Can't ping the server."/> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding Ping}" Value="true"> 
           <Setter Property="Image.Source" Value="Icons/MainWindow/StatusOK_32x.png"/> 
           <Setter Property="Image.ToolTip" Value="Successfully pinged the server."/> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Image.Style> 
      </Image> 
     </StatusBarItem> 
    </StatusBar> 
</Grid> 

至於說,我想我的路由上有所缺失(在所有未布^^),有人對這個想法?

+1

你應該調用你的'PropertyChanged'事件,而不僅僅是創建事件參數;) – lokusking

回答

0

更改您的StateLogin -Class到

public class StateLogin : INotifyPropertyChanged 
    { 
     private int _loginState; // helping variable to trigger login state change 

     public event PropertyChangedEventHandler PropertyChanged; 

     public int LoginState 
     { 
      get { return _loginState; } 
      set 
      { 
       _loginState = value; 
       OnPropertyChanged("LoginState"); 
      } 
     } 

     protected void OnPropertyChanged(string name) 
     { 
      this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 
     } 
    } 

你必須告訴你你的事件,它實際上應該火。

編輯

假設你的DataContext和其他一切設置正確,你應該嘗試一下本作您的用戶控件:

<UserControl x:Class="Tool_WPF.frmLogin" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
      x:Name="LoginRoot" 
     d:DesignHeight="300" d:DesignWidth="300"> 

    <Grid> 
     <StackPanel> 
      <TextBlock Name="labLogin" HorizontalAlignment="Center" MaxWidth="150"> 
       <TextBlock.Style> 
       <Style TargetType="TextBlock"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding ElementName=LoginRoot, Path=DataContext.LoginState}" Value="1"> 
          <Setter Property="Text" Value="Success"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
       </TextBlock.Style> 
      </TextBlock> 
      <Label HorizontalAlignment="Center">Forum username</Label> 
      <TextBox x:Name="tbUser" Width="150"></TextBox> 
      <Label HorizontalAlignment="Center">Forum password</Label> 
      <PasswordBox x:Name="tbPassword" Width="150"></PasswordBox> 
      <Button x:Name="cmdLogin" HorizontalAlignment="Center" Margin="10" Width="150" Click="cmdLogin_Click">Login</Button> 
     </StackPanel> 
    </Grid> 
</UserControl> 

沒有必要爲一個標籤,因爲你只使用一些文本。 另外,我建議你對XAML如何工作的深入探討。將內容放置爲內容絕不是一個好主意。

希望這進一步讓你。乾杯

+0

謝謝你的回覆,當然這是正確的。但不知何故,我的UserControl(在MainWindow中)仍然不更新標籤。我想我的觸發器仍然有問題。 – CuttingWide

+0

@CuttingWide修改你的用戶控件 – lokusking