创建Window.Title附加属性 [英] Creating a Window.Title Attached Property
本文介绍了创建Window.Title附加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个窗口的外壳,基本上是:
I have a Window shell that is basically:
<Window>
<ContentPresenter Content="{Binding}" />
</Window>
注入到内容presenter在运行时是用户控件。我希望能够做的是写:
Injected into the ContentPresenter at run-time are UserControls. What I want to be able to do is write:
<UserControl Window.Title="The title for my window">
[...]
</UserControl>
使窗口标题使用用户控件
Window.Title
属性更新。
我有这个可以通过附加属性来实现的感觉。任何人都可以开始我关闭了正确的方向?
I have a feeling this can be achieved using attached properties. Can anyone start me off in the right direction?
丹尼尔
推荐答案
C#:
public class MyUserControl : UserControl
{
public static readonly DependencyProperty WindowTitleProperty = DependencyProperty.RegisterAttached("WindowTitleProperty",
typeof(string), typeof(UserControl),
new FrameworkPropertyMetadata(null, WindowTitlePropertyChanged));
public static string GetWindowTitle(DependencyObject element)
{
return (string) element.GetValue(WindowTitleProperty);
}
public static void SetWindowTitle(DependencyObject element, string value)
{
element.SetValue(WindowTitleProperty, value);
}
private static void WindowTitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Application.Current.MainWindow.Title = e.NewValue;
}
}
XAML:
<UserControl namespace:MyUserControl.WindowTitle="The title for my window">
[...]
</UserControl>
这篇关于创建Window.Title附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文