如何通过包含私有内部对象的对象的接口公开该对象的依赖属性?

发布于 2024-09-27 08:49:30 字数 814 浏览 4 评论 0原文

我们有一个自定义面板类,它通过内部 DoubleAnimation 对象为其子级设置动画。但是,我们希望将动画的 Duration 依赖属性公开为面板的公共属性,以便用户在使用面板时可以在 XAML 中更改它。但我们不想暴露动画对象的任何其他部分,只想暴露持续时间。

不断向我建议的第一件事是使用 PropertyChanged 通知,但这仅适用于 setter,不适用于 getter。我们也不能简单地创建 .NET 属性,因为 XAML 完全绕过了 .NET 属性。

我的一个同事有一个聪明的想法......在外部属性和内部对象的属性之间使用双向数据绑定,这实际上看起来是一个非常简洁的解决方案。然而,除了数据绑定之外,还有另一种/更好的方法来做到这一点...通过包含对象的公共接口来公开内部对象的依赖属性吗?

更新:

看起来双向数据绑定是正确的选择。 (感谢@Jeff!)为此,我发现这是设置外部 DP 的最佳方法,因此它与内部对象的 DP 完美匹配(元数据、默认值和所有内容)!然后使用杰夫的绑定技巧,你就完成了!

public Duration Duration {
    get { return (Duration)GetValue(DurationProperty); }
    set { SetValue(DurationProperty, value); }
}

public static readonly DependencyProperty DurationProperty = DoubleAnimation.DurationProperty.AddOwner(
    typeof(SlideContentPanel));

We have a custom panel class that animates its children via an internal DoubleAnimation object. However, we want to expose the animation's Duration dependency property as a public property of our panel so the user can change it in their XAML when using our panel. But we don't want to expose any other part of the animation object, just the duration.

The first thing that keeps getting suggested to me is to use the PropertyChanged notification, but that would only work for the setter, not the getter. We also can't simply create a .NET property since XAML bypasses the .NET property altogether.

A co-worker of mine had a clever idea... use two-way data binding between the outer property and the internal object's property, which actually seems like a pretty neat solution. However, data binding aside, is there another/a better way to do this... exposing an internal object's dependency property via it's containing object's public interface?

Updated:

Looks like two-way DataBinding was the way to go. (Thanks @Jeff!) To that end, here's what I found to be the best way to set up the outer DP so it's a perfect match--metadata, defaults and all--for the inner object's DP! Then use Jeff's binding trick and you're done!

public Duration Duration {
    get { return (Duration)GetValue(DurationProperty); }
    set { SetValue(DurationProperty, value); }
}

public static readonly DependencyProperty DurationProperty = DoubleAnimation.DurationProperty.AddOwner(
    typeof(SlideContentPanel));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人哭 2024-10-04 08:49:30

试试这个...在外部对象上创建等效的依赖属性,然后从内部对象绑定到外部对象。这将在两个方向上发挥作用。

Binding durationBinding = new Binding(){
    Source = _doubleAnimation,
    Path   = new PropertyPath("Duration"),
    Mode   = BindingMode.TwoWay
};
BindingOperations.SetBinding(this, SlideContentPanel.DurationProperty, durationBinding);

对于 xaml 爱好者

<UserControl x:Class=”Controls.DataGrid.DataGrid2"
Name="rootControl">

<Grid>       
    <xcdg:DataGridControl Grid.Row="0"
       Name="internalDataGrid" 
       SelectedItem="{Binding ElementName=rootControl, Path=SelectedItem}"
       EditTriggers="{Binding ElementName=rootControl, Path=EditTriggers}"
 />

Try this... Create equivilant dependency properties on the outside objects, then bind from the inside object to the outside object. This will work in both directions.

Binding durationBinding = new Binding(){
    Source = _doubleAnimation,
    Path   = new PropertyPath("Duration"),
    Mode   = BindingMode.TwoWay
};
BindingOperations.SetBinding(this, SlideContentPanel.DurationProperty, durationBinding);

For the xaml lovers

<UserControl x:Class=”Controls.DataGrid.DataGrid2"
Name="rootControl">

<Grid>       
    <xcdg:DataGridControl Grid.Row="0"
       Name="internalDataGrid" 
       SelectedItem="{Binding ElementName=rootControl, Path=SelectedItem}"
       EditTriggers="{Binding ElementName=rootControl, Path=EditTriggers}"
 />
静谧幽蓝 2024-10-04 08:49:30

我对你的“问题”感到有些奇怪,所以这里有一些想法可以解决问题。

依赖属性可以由您的代码读取和设置。为什么不直接使用它而不是这个内部存储呢?

如果您出于性能原因想要使用内部存储值,请使用您已经说过的属性更改通知。当属性改变时,更新你的内部值。当您在内部更改属性时,请以适当的时间间隔调用依赖项属性设置器以更新其他所有人(忽略您从中收到的属性更改事件)。

也许您的情况需要除此之外的其他东西,但第二个选项应该像这样的东西一样复杂。

Something strikes me as odd about your "problem", so here are some thoughts that may clear things up.

A dependency property can be both read and set by your code. Why not just use that instead of this internal storage?

If you want to use the internal storage value for performance reasons, use the property changed notification that you already said would work. When the property changes, update your internal value. When YOU change the property internally, call the dependency property setter at appropriate intervals to update everyone else (ignoring the property change event you would receive from that).

Maybe your situation requires something other than this, but the second option ought to be as complicated as something like this should get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文