代码之家  ›  专栏  ›  技术社区  ›  James Hughes

用户控件的嵌套绑定

  •  1
  • James Hughes  · 技术社区  · 14 年前

    我很难让以下场景工作(此代码不是实际代码,但主体是相同的)。基本上,我需要将一个值从主页向下传递到一个嵌套的“可重用用户控件”,它绑定到自己的属性。我想看看“这就是它!”屏幕上的文本,但它没有在SilverLightControl2控件中设置(我怀疑是由于设置了DataContext)-但我该如何修复它?

    主页

    <Grid>
        <ContentPresenter>
            <ContentPresenter.Content>
                <Local:SilverlightControl1 OneValue="This is it!"/>
            </ContentPresenter.Content>
        </ContentPresenter>
    </Grid>
    

    银光控件1.xaml

    <Grid>
        <Local:SilverlightControl2 TwoValue="{Binding OneValue}"/>
    </Grid>
    

    Silverlight控件1.xaml.cs

    public partial class SilverlightControl1 : UserControl
    {
        public string OneValue
        {
            get { return (string)GetValue(OneValueProperty); }
            set { SetValue(OneValueProperty, value); }
        }
    
        public static readonly DependencyProperty OneValueProperty = DependencyProperty.Register(
            "OneValue", typeof(string), typeof(SilverlightControl1), new PropertyMetadata(string.Empty));
    
        public SilverlightControl1()
        {
            InitializeComponent();
            this.DataContext = this;
        }
    }
    

    Silverlight控件2.xaml

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Text="{Binding TwoValue}"  Foreground="Blue" />
    </Grid>
    

    Silverlight控件2.xaml.cs

    public partial class SilverlightControl2 : UserControl
    {
        public string TwoValue
        {
            get { return (string)GetValue(TwoValueProperty); }
            set { SetValue(TwoValueProperty, value); }
        }
    
        public static readonly DependencyProperty TwoValueProperty = DependencyProperty.Register(
            "TwoValue", typeof(string), typeof(SilverlightControl2), new PropertyMetadata(string.Empty));
    
    
        public SilverlightControl2()
        {
            InitializeComponent();
            this.DataContext = this;
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   AnthonyWJones    14 年前

    一旦你觉得有必要这样做:

    this.DataContext = this; 
    

    知道你可能搞错了。这可能是我希望在Silverlight特定的“臭味列表”上找到的第一件事。

    在这种情况下,你是专门的 UserControl 更好的方法是这样做:

    银光控件1.xaml

    <Grid>         
        <Local:SilverlightControl2 x:Name="MyControl2" />         
    </Grid>
    

    silverlightcontrol1.xaml.cs(我只是向构造器展示剩余的部分,和您拥有的一样)

    public SilverlightControl1()  
    {  
        InitializeComponent();  
        MyControl2.SetBinding(SilverlightControl2.TwoValueProperty , new Binding("OneValue") { Source = this });
    
    } 
    

    Silverlight控件2.xaml

    <Grid x:Name="LayoutRoot" Background="White">               
        <TextBlock x:Name="MyTextBox"  Foreground="Blue" />               
    </Grid> 
    

    silverlightcontrol1.xaml.cs(我只是向构造器展示剩余的部分,和您拥有的一样)

    public SilverlightControl2()  
    {  
        InitializeComponent();  
        MyTextBox.SetBinding(TextBox.TextProperty , new Binding("TwoValue") { Source = this });  
    } 
    

    因为在 UserControls 您知道XAML的结构,并且可以在代码中命名需要访问的元素,您可以使用一行代码来创建绑定。 这使得DataContext可以自由地执行设计目的,而不是出于不同的目的而被高估。

    另一种方法不是专门化 用户控制 您创建了一个模板化控件,在这种情况下,可以使用如下内容在XAML中表示绑定:

    <TextBox Text="{TemplateBinding TwoValue}" />
    

    模板绑定仅适用于 ControlTemplate 所以你不能在 用户控制 .