다음에 필립 수미의 코드를 사용한다
public static class WindowTitleBehavior
{
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)
{
UserControl control = d as UserControl;
if (!control.IsLoaded)
{
control.Loaded += new RoutedEventHandler(setTitle);
}
setTitle(control);
}
private static void setTitle(object sender, RoutedEventArgs e)
{
UserControl control = sender as UserControl;
setTitle(control);
control.Loaded -= new RoutedEventHandler(setTitle);
}
private static void setTitle(UserControl c)
{
Window parent = UIHelper.FindAncestor<Window>(c);
if (parent != null)
{
parent.Title = (string)WindowTitleBehavior.GetWindowTitle(c);
}
}
}
첫 번째 찾기 조상 창 : 내보기에 http://www.hardcodet.net/2008/02/find-wpf-parent
내가 지금 할 수있는 :
<UserControl Behaviors:WindowTitleBehavior.WindowTitle="My Window Title">
그리고이 포함 된 윈도우의 제목을 설정합니다.
'Window.Title'을 할 수 없다고 생각합니다. 즉, 기존 클래스에 대해 연결된 속성을 만듭니다. 당신이 할 수있는 것은 창 제목을 설정하는 속성 인 MyCustomControl.WindowTitle을 생성하는 것입니다 (그리고 임의의 컨트롤에 첨부 될 수 있습니다). – Heinzi