【问题标题】:Using Databinding in DependencyProperty在 DependencyProperty 中使用数据绑定
【发布时间】:2019-09-07 11:06:05
【问题描述】:

我创建了一个继承自Window 的类,并有一个名为TitlebarContent 的DependencyProperty

public FrameworkElement TitleBarContent
{
    get { return (FrameworkElement)GetValue(TitleBarContentProperty); }
    set { SetValue(TitleBarContentProperty, value); }
}

// Using a DependencyProperty as the backing store for TitleBarContent.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleBarContentProperty =
    DependencyProperty.Register("TitleBarContent", typeof(FrameworkElement), typeof(tkWindowControl), new PropertyMetadata(default(FrameworkElement)));

在样式的ResourceDictionary 中,我为此属性添加了ContentControl。 现在我想在另一个应用程序中使用新的“WindowObject”并访问新的TitlebarContentProperty。我可以看到标题栏中的项目,我可以移动Window,调整它的大小等等。但我无法绑定到这些项目。例如,我想在标题栏中添加一个帮助按钮。 Button 已显示,但我无法单击它。

    <tk:tkWindowControl.TitleBarContent>
        <DockPanel Grid.Row="0"  FlowDirection="LeftToRight">
            <Button Content="Exit" Command="{Binding ExitApplicationCommand}" Height="60" Width="60" Background="Red"/>
        </DockPanel>
    </tk:tkWindowControl.TitleBarContent>

我的DependencyPropertytypeof(FrameWorkElement),因为我喜欢在标题栏中添加几个Buttons

可以这样使用我的绑定吗?

【问题讨论】:

  • 如果您的按钮出现在 UI 中,那么我希望您需要做的就是在窗口的数据上下文中拥有一个公共 iCommand 属性 ExitApplicationCommand。你现在有什么?
  • 背景已经完成。我通过将按钮放在普通网格中来尝试绑定。一切正常。但是在Titlebarcontent 中使用的按钮没有IsMouseOverEffect 并且Binding 不再起作用。

标签: c# wpf xaml data-binding dependency-properties


【解决方案1】:

Button或父元素的WindowChrome.IsHitTestVisibleInChrome附加属性设置为true

<Button Content="Exit" WindowChrome.IsHitTestVisibleInChrome="True" ... />

或者,您应该减小WindowChromeCaptionHeight 属性的值。

【讨论】:

  • 你又一次帮助了我@mm8。现在我完成了我的 UI。谢谢
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 2011-01-05
  • 2011-04-03
  • 2020-02-01
相关资源
最近更新 更多