我试图将属性绑定到文本块,但没有成功。我有一个“ Notifier”类,其中包含INotifyPropertyChanged事件。模型类继承自“ Notifier”类。
namespace MyOffice.Models
{public class Notifier : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
System.Diagnostics.Debug.WriteLine("OnProperty called");
if (PropertyChanged!=null)
{
System.Diagnostics.Debug.WriteLine("PropertyChanged called");
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
“在线”类如下:
namespace MyOffice.Models
{
public class Online : Notifier
{
private string _dbUser;
internal string DbUser { get; set; }
internal System.Security.SecureString DbPass { get; set; }
private string _progressMessage;
public string ProgressMessage
{
get => _progressMessage;
set
{
_progressMessage = value;
System.Diagnostics.Debug.WriteLine("progress message changed");
OnPropertyChanged("ProgressMessage");
}
}
}
}
“全部”类充当在线和其他各种模型类的嵌套。
namespace MyOffice.Models
{
public class AllModels : Notifier
{
public Doc Doc {get; set;}
public Online Online {get; set;}
public AllModels()
{
Doc = new Doc();
Online = new Online();
}
}
}
这是xaml:
<Window x:Class="MyOffice.MainWindow"
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"
mc:Ignorable="d"
Title="blahblah" Height="768" Width="1366" FontSize="16" FontFamily="Arial" WindowStartupLocation="CenterScreen" WindowState="Maximized" Cursor="Arrow" Icon="Resources/medical-bag.ico" Background="DarkCyan" Closing="MainWin_Closing" WindowStyle="None" DataContext="All">
<Window.Resources>
<Style x:Key="leftPanelButtons" TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="Georgia"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Margin" Value="0,0,0,0"/>
<Setter Property="Background" Value="ForestGreen"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="ForestGreen" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkSeaGreen"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="upperPanelButtons" TargetType="{x:Type Button}">
<Setter Property="Background" Value="DarkCyan"/>
<Setter Property="FontFamily" Value="Georgia"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Margin" Value="0,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="DarkCyan" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkTurquoise"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="exitButtons" TargetType="{x:Type Button}">
<Setter Property="Background" Value="DarkRed"/>
<Setter Property="FontFamily" Value="Georgia"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Margin" Value="0,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="DarkRed" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="IndianRed"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="invisibleTabs" TargetType="{x:Type TabItem}">
<Setter Property="Visibility" Value="Hidden"/>
<Setter Property="Height" Value="0"/>
<Setter Property="Margin" Value="0,0,0,0"/>
</Style>
</Window.Resources>
<Grid x:Name="gridMain" Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="46" />
<!--<RowDefinition Height="35"/>-->
<RowDefinition Height="*"/>
<RowDefinition Height="35"/>
</Grid.RowDefinitions>
<!--<TextBlock Grid.Row="0" x:Name="txbName" HorizontalAlignment="Left" Margin="5,5,0,0" TextWrapping="Wrap" Text="Όνοματεπώνυμο" VerticalAlignment="Top" Height="18" Width="130"/>-->
<UniformGrid Grid.Row="0" Width="Auto" Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Rows="1">
<UniformGrid.Resources>
<Style BasedOn="{StaticResource upperPanelButtons}" TargetType="Button"/>
</UniformGrid.Resources>
<Button Content="1" Click="Button_Click_1"/>
<Button Content="2" Click="Button_Click_2"/>
<Button Content="3" Click="Button_Click_3"/>
<Button Content="4" Click="Button_Click_4"/>
<Button Content="5" Click="Button_5"/>
<Button Content="6" Style="{StaticResource exitButtons}" Click="Button_Click_UpperPanelExit"/>
</UniformGrid>
<DockPanel Grid.Row="1">
<DockPanel Height="Auto" Width="Auto" VerticalAlignment="Stretch">
<UniformGrid Columns="1" Height="Auto" VerticalAlignment="Stretch" MinWidth="150" Width="Auto">
<UniformGrid.Resources>
<Style BasedOn="{StaticResource leftPanelButtons}" TargetType="Button"/>
</UniformGrid.Resources>
<Button Click="Click_Button_asdfasdf">
aadfasdf
</Button>
<Button Click="Click_Button_efefefe">
efefefe
</Button>
<Button Click="Click_Button_t5t5t5t">
t5t5t5t5t5
</Button>
<Button Click="Click_Button_jujujujuj">
jujujujuju
</Button>
<Button Click="Click_Button_lklklklkl">
klklklklkl
</Button>
<Button Click="Click_Button_dkdkdkd">
dkdkdkdk
</Button>
<Button Click="Click_Button_fawf">
wefwef
</Button>
</UniformGrid>
<DockPanel Height="Auto" Width="Auto" Background="PaleGoldenRod" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0">
<Grid>
<Frame x:Name="mainFrame"></Frame>
</Grid>
</DockPanel>
</DockPanel>
</DockPanel>
<Border Grid.Row="2" Margin="5,0,5,0" Height="24" Width="1366" >
<TextBlock x:Name="txbProgress" TextWrapping="NoWrap" Width="1366" Background="DarkCyan" Foreground="White" Text="{Binding Path=All.Online.ProgressMessage, Mode=OneWay}" TextAlignment="Center"/>
</Border>
</Grid>
</Window>
然后-最后-这是MainWindow代码:
namespace MyOffice
{
public partial class MainWindow : Window
{
public Models.AllModels All {get; set;}
public MainWindow()
{
InitializeComponent();
All = new Models.AllModels();
DataContext = All;
All.Online.ProgressMessage = "progressing...";
}
}
}
每当更改属性(progressMessage)时,都会运行“ OnPropertyChanged”方法,但“ PropertyChanged”始终为空...并且执行永远不会达到以下语句:
PropertyChanged(this, new PropertyChangedEventArgs(name));
因此,文本块从不显示任何“ progressMessage”。为什么总是为空?程序有问题吗?请注意,我已经从内部更改为公共(类和相关属性),但仍然无法使用...