2016-07-23 5 views
0

に変更しましたので、私は他の要素をグループ化しているユーザーコントロールを(今のところ)作成しています。ユーザーコントロール内のプロパティが変更されたときにビューを更新したいと思います。私はInotifyを実装しようとしましたが、それは一見すると動作します(私はそれを私のメインフォームで同じ方法で行っています、それは問題ありません)。また、XAMLでバインディングを設定しようとしましたが、何とかそれを通過できません。私はルーティングされたイベントを実装しなければならないと思うだろうが、その場合にはどうすればよいのか分からない。C#WPFのルートイベントをユーザーコントロールからメインフォーム

私はOnPropertyChangedメソッドからいくつかのものを取り除くことができましたが、割り当てられていない対応するイベントを削除したい場合は、INotifyを実装していないことを示しています。ユーザーコントロールから

XAML:ユーザーコントロール(プロパティクラス)

 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コード

の一部をするためのコードビハインド

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

<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あなたのUserControlを変更しました。 – lokusking

関連する問題