【问题标题】:binding to a usercontrol not working绑定到用户控件不起作用
【发布时间】:2012-02-11 12:57:13
【问题描述】:

我遇到了数据绑定问题, 所以我创建了一个用户控件

UserControl1.xaml.cs

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }
    public static DependencyProperty TestThisProperty = DependencyProperty.Register
        (
        "TestThis",
        typeof(string),
        typeof(UserControl1),
        new PropertyMetadata("Some Data",new PropertyChangedCallback(textChangedCallBack))
        );
    public string TestThis
    {
        get { return (string)GetValue(TestThisProperty); }
        set { SetValue(TestThisProperty, value); }
    }
    static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
        UserControl1 _us = (UserControl1)property;

        _us.MyUserControl.TestThis = (string)args.NewValue;

    }
}

UserControl1.xaml

<UserControl x:Class="WpfApplication1.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" 
         mc:Ignorable="d"  x:Name="MyUserControl"

         d:DesignHeight="300" d:DesignWidth="300">
          </UserControl>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    UserControl1 _uc = new UserControl1();

    public MainWindow()
    {
        InitializeComponent();
        DispatcherTimer _dt = new DispatcherTimer();
        _dt.Interval = new TimeSpan(0, 0, 1);
        _dt.Start();
        _dt.Tick += new EventHandler(_dt_Tick);
    }
    private void _dt_Tick(object s,EventArgs e)
    {
        _uc.TestThis = DateTime.Now.ToString("hh:mm:ss");

    }

最后是 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"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">

<Grid>
    <TextBox Text="{Binding Path=TestThis,ElementName=MyUserControl, UpdateSourceTrigger=PropertyChanged}"/>

  </Grid>

但是这里的问题是,当我调试我的代码时,我收到了这个警告 找不到与引用“ElementName=MyUserControl”进行绑定的源。而且ui当然不会更新,请问有什么想法吗?

【问题讨论】:

    标签: wpf data-binding user-controls


    【解决方案1】:

    ElementName 仅针对同一 namescope 中的名称,例如

    <TextBox Name="tb"/>
    <Button Content="{Binding Text, ElementName=tb}"/>
    

    如果您没有在 MainWindow XAML 中定义 UserControl 并在那里给它一个名称或在代码后面(和目标)中给它一个名称或 register a name,您将无法让这个 ElementName 绑定工作。

    另一种方法是将用户控件公开为 MainWindow 上的属性,例如

    public UserControl1 UC { get { return _uc; } }
    

    然后你可以这样绑定:

    {Binding UC.TestThis, RelativeSource={RelativeSource AncestorType=Window}}
    

    【讨论】:

      猜你喜欢
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      相关资源
      最近更新 更多