2016-07-23 38 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