所以我编码具有(现在)的应用程序,其中我分组一些其它元件的一个用户控制。我想在用户控件内的属性更改时更新视图。我试图实现的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>
至于说,我想我的路由上有所缺失(在所有未布^^),有人对这个想法?
你应该调用你的'PropertyChanged'事件,而不仅仅是创建事件参数;) – lokusking