C# 绑定不适用于已更改的属性
我正在尝试用xaml将属性绑定到SplashScreen中的文本块。textblock应该显示属性的内容。类处理程序具有属性“Shares”,在splashscreen的xaml代码中,我编写了以下代码:C# 绑定不适用于已更改的属性,c#,wpf,C#,Wpf,我正在尝试用xaml将属性绑定到SplashScreen中的文本块。textblock应该显示属性的内容。类处理程序具有属性“Shares”,在splashscreen的xaml代码中,我编写了以下代码: <UserControl x:Class="DXApplication3.WaitScreen" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.micro
<UserControl
x:Class="DXApplication3.WaitScreen"
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:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:local="clr-namespace:DXApplication3"
mc:Ignorable="d"
d:DataContext="{x:Static dx:SplashScreenViewModel.DesignTimeData}">
<UserControl.Resources>
<local:Handler x:Key="Shares"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Height="455" Width="859">
<Grid x:Name="Splash" Width="450" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0">
<Grid x:Name="Back">
<Border Background="Black" CornerRadius="3" Opacity="0.15"/>
<Border CornerRadius="2" Margin="-200,2,-201,-257" Background="White"/>
</Grid>
<Image x:Name="Logotype" DockPanel.Dock="Right" Source="../DXSplashScreen/DTILogo.png" Stretch="None"
HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" Margin="0,24,0,51" Width="253" >
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform X="-413" Y="-55"/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<Grid x:Name="Content_Area" Margin="12">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<dx:WaitIndicator Content="Loading..." DeferedVisibility="true" Margin="0,120,0,-52" Grid.RowSpan="4">
<dx:WaitIndicator.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="Please Wait" FontSize="20"/>
<TextBlock Text="ShareAnalyzer is running..."/>
</StackPanel>
</DataTemplate>
</dx:WaitIndicator.ContentTemplate>
</dx:WaitIndicator>
<DockPanel x:Name="Footer" Grid.Row="3" Margin="-181,115,205,-91">
<TextBlock x:Name="TextHeader" TextWrapping="Wrap" Text="Analyzed shares" Opacity="1" Foreground="#FF2D2D2D" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,2,0,-2" FontSize="16" FontWeight="Bold" RenderTransformOrigin="0.5,0.5">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform X="2" Y="28"/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock x:Name="SharesHeader" TextWrapping="Wrap" Text="{Binding Source={StaticResource Shares}, Path=Shares, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Opacity="1" Foreground="#FF2D2D2D" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,4,-87,1" FontSize="13.333" Width="124">
<TextBlock.RenderTransform>
如果单击Main Window.cs中的按钮,飞溅屏幕将启动。但问题是,在sart之后,splashscreen中的文本块是emty
有人能帮我吗?
谢谢问题在于您正在绑定到一个静态资源(您命名为
共享
),但正在更改一个不同的属性(根据您的注释中的代码,一个名为处理程序
)
因此,您定义了以下内容:
<UserControl.Resources>
<local:Handler x:Key="Shares"/>
</UserControl.Resources>
其中此
是您的窗口(或用户控件
,或您在其中定义资源的位置)对象。如您所见,请检查空值
奖金
正如其他人在注释中所述,将事件变量复制到本地变量的全部目的是“冻结”它,因此如果其他线程在null检查和实际调用之间修改事件,您将使用未修改的副本
因此,你的:
if (PropertyChanged != null)
{
PropertyChangedEventHandler propertyChanged = PropertyChanged;
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
应该是:
PropertyChangedEventHandler propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
但是,如果您使用的是C#6(Visual Studio 2015),则可以使用null条件运算符,这将对您起到同样的作用,并将其更改为:
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
尝试将您的类设置为public,而不是使用Text={StaticResource…}use Text={Binding Shares}我尝试过,但仍然不起作用..如何填充Shares属性?在处理程序类的构造函数中,为Shares属性分配一些值,然后运行。我尝试像这样在MainWindow.xaml.cs中为Handler.Shares设置一些值,不需要此行:PropertyChangedEventHandler propertyChanged=propertyChanged;在下一行中,将p更改为propertyChanged(…)中的大写字母p。
(this.Resources["Shares"] as Handler).Shares = "Test 1";
if (PropertyChanged != null)
{
PropertyChangedEventHandler propertyChanged = PropertyChanged;
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
PropertyChangedEventHandler propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));