none
WPF 用户控件内控件的绑定问题 RRS feed

  • 问题

  • using System.Windows;
    using System.Windows.Controls;
    
    namespace WpfApp1
    {
        /// <summary>
        /// UserControl1.xaml 的交互逻辑
        /// </summary>
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }
    
            public new object Content
            {
                get { return (object)GetValue(ContentProperty); }
                set { SetValue(ContentProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
            public new static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(object), typeof(UserControl1), new PropertyMetadata(null, (s, e) =>
            {
                var resizableView = s as UserControl1;
                if (e != null && resizableView != null)
                {
                    resizableView.ContentControl.Content = e.NewValue;
                }
            }));
    
        }
    }


    <UserControl x:Class="WpfApp1.UserControl1"
                 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" 
                 xmlns:local="clr-namespace:WpfApp1"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <Grid>
            <ContentControl x:Name="ContentControl" />
        </Grid>
    </UserControl>
    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow"
            Height="450"
            Width="800">
        <StackPanel>
            <TextBox Name="textbox"
                     Width="100"
                     Height="30"
                     BorderBrush="Black"
                     BorderThickness="1" />
            <ContentControl>
                <!--<TextBlock Text="{Binding ElementName=textbox,Path=Text}"
                           Width="100"
                           Height="30" />-->
                <local:UserControl1>
                        <TextBlock Text="{Binding ElementName=textbox,Path=Text}"
                                   Width="100"
                                   Height="30" />
                </local:UserControl1>
            </ContentControl>
        </StackPanel>
    </Window>

    以上分别为usercontrol的前后台代码和页面代码,

    外面嵌套了usercontrol控件后就不能绑定了





    2020年8月14日 7:10

全部回复

  • 你好,

    你可以使用在UserControl1中定义得Content属性对自定义得控件绑定值,代码如下:

    <local:UserControl1 Content="{Binding ElementName=textbox,Path=Text}"></local:UserControl1>

    这样你就可以使用ElementName对自定义控件绑定值了。

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年8月17日 2:27