jef*_*ora 7 .net c# wpf datatemplate
我有一个DataTemplate由是源自媒体元素控制的MediaElementBase从WPF媒体工具库.该MediaElementBase类提供了两个属性,LoadedBehavior并UnloadedBehavior允许用户指定当元素加载/卸载会发生什么.
我发现在a DataTemplate(如下)中使用它时,这些属性在卸载模板时会重置为默认值,但在Unloaded调用事件之前,意味着只UnloadedBehavior执行默认值:
<DataTemplate DataType="{x:Type Channels:AnalogChannel}">
<Controls:AnalogTvGraphFileElement
LoadedBehavior="Play"
UnloadedBehavior="Stop"
Channel="{Binding}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
当控件只是页面上的元素并Unloaded通过正常的导航事件发生时,不会发生这种情况.
调试DependencyPropertyChanged EventHandler显示内部方法System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(在PresentationFramework.dll中)检查是否DependencyProperty可能继承,如果不是,则使其无效.果然,更改LoadedBehavior/ UnloadedBehavior添加的属性元数据会FrameworkPropertyMetadataOptions.Inherits在模板更改时阻止此属性重置.
有谁知道为什么会这样?我可以添加Inherits标志作为解决方法,因为此元素没有受此影响的子元素,但我想知道为什么/是否正确的做法.
如果您正在了解有关我正在做什么以及为什么要更改的更多信息,DataTemplates您可以查看此问题以获取说明.
您是否尝试创建包含AnalogTvGraphFileElement元素的用户控件,然后在模板中使用该用户控件,而不是将此元素直接放入模板中?
If the problem here is being caused by the template systems going and unsetting properties that it set in the first place, moving your element into a user control should help, because the properties would no longer be being set from the template.
As for why you're seeing the behaviour in the first place, as far as I can tell the relative ordering of the Unloaded event and the loss of properties set via the template is not documented, so you shouldn't depend on any particular ordering. The fact that you will lose the property values is documented. (Or at least, it's implicit from the docs.) The WPF property system treats local values in a template as being a different sort of thing than normal local values outside of a template. See this MSDN page on dependency property precedence - 4b indicates that local property sets in the template are not the same thing as local properties. (It seems odd to make a distinction, but it should be possible to set property values from both sources by setting them in the template - type 4b - and then at runtime, going and finding the element in a particular instance of the template and setting its local value from code - type 3. And for that scenario you really would want type 3 local values to have higher precedence than type 4b local values.)
Weird as that seems, it might make more sense when you consider that a single template may be providing values for multiple instances. You've got just one local property setter that could affect any number of elements. (This means that a simple mental model of a template as being a factory that builds a visual tree and sets properties on that tree would be wrong. It's a factory that builds a visual tree, but it doesn't set properties. The property system simply ensures that in the absence of any higher-precedence property value sources, the elements in a visual tree that's the template for something will pick up values from setters in the template.)
That page does just about tell you that type 4b properties will disappear once the template ceases to be active - the template is no longer the template for the templated parent, and so any local values provided by that template no longer qualify as candidate values of type 4b (or any other type for that matter) for the property. In short, once the template ceases to be the template for something, it ceases to have any business providing values for that thing or anything in it. This implies that the visual tree for an instance of a template enters a weird limbo state in which it is no longer the template for anything but hasn't yet unloaded.
Of course, it doesn't seem useful for the template to stop providing values before the relevant visual tree has finished unloading, but perhaps properties whose value is only significant when an element unloads are a bit of a weird case, and not one that was specifically designed for. Thinking about it, they probably shouldn't ever be dependency properties - almost all the features that make DPs useful don't really mean much once you're unloaded from the visual tree. So arguably, it's a bug in the Media Kit that UnloadedBehaviour is a DP in the first place.
继承属性(类型10)比本地模板属性集(类型4b)更晚关闭是不一致的,但是我不确定在这里期望任何特定顺序是合理的.文档暗示一个订单或另一个订单并不明显,因此任何订单都是正确的......并且WPF似乎通过在一个场景中选择一个订单而在另一个场景中选择另一个订单来利用它.