[英]WPF: initial visibility and template loading
我遇到了一个问题,如果初始化该控件时看不到该控件,则该控件上的绑定设置不正确。 我已使用以下精简控件复制了该问题:
public class Test3 : Control
{
static Test3()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Test3), new FrameworkPropertyMetadata(typeof(Test3)));
}
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register("Test", typeof(string),
typeof(Test3), new UIPropertyMetadata("test3 default text"));
}
public class Test2 : Control
{
static Test2()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Test2), new FrameworkPropertyMetadata(typeof(Test2)));
}
public FrameworkElement Test3Control
{
get { return (FrameworkElement)GetValue(Test3ControlProperty); }
set { SetValue(Test3ControlProperty, value); }
}
public static readonly DependencyProperty Test3ControlProperty =
DependencyProperty.Register("Test3Control", typeof(FrameworkElement),
typeof(Test2), new UIPropertyMetadata(null));
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register("Test", typeof(string), typeof(Test2),
new UIPropertyMetadata("test2 default text"));
}
public partial class Test1 : UserControl
{
public Test1()
{
InitializeComponent();
}
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register("Test", typeof(string),
typeof(Test1), new UIPropertyMetadata("test1 default text"));
}
用户控制的XAML:
<UserControl x:Class="Test.Test1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dummy="clr-namespace:WpfTestApplication.Test"
Name="ucThis">
<UserControl.Resources>
<Style TargetType="{x:Type test:Test2}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type test:Test2}">
<GroupBox Header="Test2">
<StackPanel>
<TextBox IsEnabled="False" Text="{TemplateBinding Test}"/>
<GroupBox Header="Test3">
<ContentPresenter Content="{TemplateBinding Test3Control}"/>
</GroupBox>
</StackPanel>
</GroupBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<test:Test2 Test="{Binding ElementName=ucThis,Path=Test}">
<test:Test2.Test3Control>
<test:Test3 Test="{Binding ElementName=ucThis,Path=Test}">
<test:Test3.Template>
<ControlTemplate TargetType="{x:Type test:Test3}">
<TextBox IsEnabled="False" Text="{TemplateBinding Test}"/>
</ControlTemplate>
</test:Test3.Template>
</test:Test3>
</test:Test2.Test3Control>
</test:Test2>
</Grid>
</UserControl>
...和XAML用于主窗口(无论如何,它的胆量):
<DockPanel>
<StackPanel>
<TextBox Name="tbInput"/>
<Expander Header="Initially Visible" IsExpanded="True">
<test:Test1 Test="{Binding ElementName=tbInput, Path=Text}" />
</Expander>
<Expander Header="Initially Collapsed" IsExpanded="False">
<test:Test1 Test="{Binding ElementName=tbInput, Path=Text}" />
</Expander>
</StackPanel>
</DockPanel>
我希望输入到文本框的任何文本(“ tbInput”)都将显示在Test2和Test3框中-实际上,这对于Test2的两个实例都是如此,但仅对于最初可见的Test3。 最初折叠的Test3始终显示默认文本,即使在输入文本时可见。
我尝试使用Snoop进行调查,但是当我评估带有Snoop的树的相关部分时,该问题得以纠正,因此并没有太大帮助。
是什么导致此行为? 我该如何纠正? 我在哪里可以了解更多信息?
更新:
通过查看输出窗口,我发现了以下错误消息: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=ucThis'. BindingExpression:Path=Test; DataItem=null; target element is 'Dummy3' (Name=''); target property is 'Test' (type 'String')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=ucThis'. BindingExpression:Path=Test; DataItem=null; target element is 'Dummy3' (Name=''); target property is 'Test' (type 'String')
通过处理这些控件上的已加载和初始化事件,我可以看到在启动时加载了Dummy1和Dummy2之后会发生此错误。 假人3在可见之前不会加载。
我认为问题在于,由于尚未将模板应用于折叠的Test3
实例,因此尚未将其插入可视化树中。 因此,在创建绑定时,它不在外部Test1
实例的名称范围内,因此无法解析为ElementName
指定的名称ucThis
。
您可以通过将Test3Control
添加到Test2
的逻辑树中来解决此问题。 尝试如下修改依赖项属性定义:
public static readonly DependencyProperty Test3ControlProperty =
DependencyProperty.Register("Test3Control", typeof(FrameworkElement),
typeof(Test2),
new UIPropertyMetadata(null, OnTest3ControlPropertyChanged));
private static void OnTest3ControlPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var source = (Test2)d;
var oldValue = e.OldValue as FrameworkElement;
var newValue = e.NewValue as FrameworkElement;
if (oldValue != null)
source.RemoveLogicalChild(oldValue);
if (newValue != null)
source.AddLogicalChild(newValue);
}
在大多数情况下,将新的基于UIElement
的属性添加到控件时,您将要确保将其添加到逻辑树中。 这不会自动发生。 因此,它也不会自动添加到可视化树中。 在这种情况下,它仅被加载到可视树中,因为它已明确插入模板中。
看一下WPF核心控件的内部结构,例如Decorator
( Border
派生自其)和其Child
属性,以了解定义新控件类型时可能需要哪种类型的管道。
还要注意有多少个“子控件”属性不是依赖项属性。 基于Visual
的依赖项属性很容易出现问题,尤其是当您尝试通过setter更改它们或设置它们的动画时。 我发现最好是通过简单地将子控件公开为常规CLR属性来阻止开发人员滥用这些属性并为自己制造麻烦。
我不确定您的特定问题,但我会将Test2的隐式样式定义移至必须位于“主题”文件夹中的Generic.xaml模块中。 框架将自动扫描该文件以查找隐式样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.