2

我已经实现了我自己的简单版本的导航窗口,主要是因为导航窗口日志不能让我控制可以存在多少个孩子。所以我在窗口内使用边框并每次都改变它的孩子。作为孩子,我正在使用 UserControl。我想将我的 Window 的标题绑定到我当前孩子的 Title 属性。不知何故,我想不出办法来做到这一点。

主窗口 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");
        }
    }
}

用户控件 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>

后面的用户控制代码:

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 内部的绑定也不起作用。我究竟做错了什么?

4

2 回答 2

1

问题是 a 的Child属性Border不是 aDependencyProperty所以没有更改通知。每次更改时都必须Binding手动更新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();
    }
}
于 2012-06-21T07:56:09.313 回答
0

我不确定你为什么要做你正在做的事情,但关于你的问题:

将“来源”更改为“相对来源”

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

那应该解决绑定问题

编辑:

当您真的想这样做时,您可以将borderContent 元素设为ContentControl 并改用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>
于 2012-06-21T07:45:09.147 回答