在 DependencyProperty 中使用数据绑定
Using Databinding in DependencyProperty
我创建了一个继承自 Window
的 class,并且有一个名为 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>
我的 DependencyProperty
是 typeof(FrameWorkElement)
因为我想在标题栏中添加几个 Buttons
。
可以这样使用我的绑定吗?
将 Button
或父元素的 WindowChrome.IsHitTestVisibleInChrome
附加 属性 设置为 true
:
<Button Content="Exit" WindowChrome.IsHitTestVisibleInChrome="True" ... />
或者,您应该减小 WindowChrome
的 CaptionHeight
属性 的值。
我创建了一个继承自 Window
的 class,并且有一个名为 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>
我的 DependencyProperty
是 typeof(FrameWorkElement)
因为我想在标题栏中添加几个 Buttons
。
可以这样使用我的绑定吗?
将 Button
或父元素的 WindowChrome.IsHitTestVisibleInChrome
附加 属性 设置为 true
:
<Button Content="Exit" WindowChrome.IsHitTestVisibleInChrome="True" ... />
或者,您应该减小 WindowChrome
的 CaptionHeight
属性 的值。