2012-06-21 3 views
1

내비게이션 창의 저널 버전은 몇 개의 자녀가 존재할 수 있는지를 제어 할 수 없기 때문에 내 자신의 간단한 탐색 버전을 구현했습니다. 그래서 나는 창 안쪽에 경계선을 사용하고 매번 그 자식을 바꾼다. 자녀로서 저는 UserControl을 사용하고 있습니다. 내 Window의 제목을 현재 자식의 Title 속성에 바인딩하고 싶습니다. 어쨌든 나는 그것을 할 길을 찾지 못합니다.테두리의 자식 속성에 바인딩

MainWindow를 XAML :

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="525" 
    Height="350" 
    Background="AliceBlue" 
    Title="{Binding Path=Child.Title, 
        ElementName=borderContent}"> 
<DockPanel> 
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal"> 
     <Button Content="&lt;-" x:Name="btnBack" /> 
     <Button Content="->" x:Name="btnForward" /> 
    </StackPanel> 
    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal"> 
     <Button Content="1" Click="Button_Click_1" /> 
     <Button Content="2" Click="Button_Click_2" /> 
    </StackPanel> 
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> 
     <Border x:Name="borderContent" /> 
    </ScrollViewer> 
</DockPanel> 

MainWindow를 코드 숨김

using System; 
using System.Windows; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click_1(object sender, RoutedEventArgs e) 
     { 
      this.borderContent.Child = new ContentPage("Title 1"); 
     } 

     private void Button_Click_2(object sender, RoutedEventArgs e) 
     { 
      this.borderContent.Child = new ContentPage("TITLE 2"); 
     } 
    } 
} 

UserControl을 XAML :

<UserControl x:Class="WpfApplication1.ContentPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid> 
    <TextBlock Text="{Binding Source={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Title}" /> 
</Grid> 

,691 뒤에 363,210

사용자 컨트롤 코드 :

using System.Windows; 
using System.Windows.Controls; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for Content.xaml 
    /// </summary> 
    public partial class ContentPage : UserControl 
    { 
     public string Title 
     { 
      get { return (string)this.GetValue(ContentPage.TitleProperty); } 
      set { this.SetValue(ContentPage.TitleProperty, value); } 
     } 

     // Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty TitleProperty = 
      DependencyProperty.Register("Title", typeof(string), typeof(ContentPage), new UIPropertyMetadata(string.Empty)); 

     public ContentPage(string Title) 
     { 
      this.Title = Title; 
      InitializeComponent(); 

     } 
    } 
} 

는 어떻게 든도 작동하지 않는 UserControl을 내부 바인딩. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

1

문제는 BorderChild 속성이 그래서 변경 알림이없는 DependencyProperty되지 않는 것입니다. 당신은 물론이 만하여 MainWindow를 바인딩하지, UserControl을 바인딩 해결할 것, 죄송합니다 .... 난 그냥 눈치 Child

private void Button_Click_1(object sender, RoutedEventArgs e) 
{ 
    this.borderContent.Child = new ContentPage("Title 1"); 
    UpdateTitleBindingExpression(); 
} 

private void Button_Click_2(object sender, RoutedEventArgs e) 
{ 
    this.borderContent.Child = new ContentPage("TITLE 2"); 
    UpdateTitleBindingExpression(); 
} 
private void UpdateTitleBindingExpression() 
{ 
    BindingExpressionBase beb = BindingOperations.GetBindingExpressionBase(this, Window.TitleProperty); 
    if (beb != null) 
    { 
     beb.UpdateTarget(); 
    } 
} 
0

난 당신이 귀하의 질문에 당신이하고있는 일을하지만, 대한 이유를 잘 모르겠어요 :

변화 "소스"를 "RelativeSource를"바인딩 문제를 해결해야

<Grid> 
    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Title}" /> 
</Grid> 

편집 : 당신이 정말로 그것을 그런 식으로하고 싶지

, 당신은 borderContent 요소 계속 만들 수 entControl을 사용하고 대신 Content 속성을 사용하십시오. 이것은 당신이 작동합니다 바인딩하고하는 DependencyProperty이기 때문에 :

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Width="525" 
     Height="350" 
     Background="AliceBlue" 
     Title="{Binding Content.Title, ElementName=borderContent}"> 
    <DockPanel> 
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal"> 
     <Button Content="&lt;-" x:Name="btnBack" /> 
     <Button Content="->" x:Name="btnForward" /> 
    </StackPanel> 
    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal"> 
     <Button Content="1" Click="Button_Click_1" /> 
     <Button Content="2" Click="Button_Click_2" /> 
    </StackPanel> 
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> 
     <ContentControl x:Name="borderContent" /> 
    </ScrollViewer> 
    </DockPanel> 
</Window> 
+0

을 변경 수동으로 매번 Binding를 업데이트해야합니다. – harri

+0

DependencyProperty가 아니기 때문에 경계의 Child 속성에 바인딩 할 수 없습니다. 따라서 바인딩은 업데이트되지 않습니다. – harri