C# 从XAML到代码隐藏的数据绑定

C# 从XAML到代码隐藏的数据绑定,c#,.net,wpf,data-binding,binding,C#,.net,Wpf,Data Binding,Binding,我在代码隐藏中有这个Textdependency属性: public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MainWindow), new PropertyMetadata("Hello world")); public string Text { get { return (string)G

我在代码隐藏中有这个
Text
dependency属性:

public static DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(MainWindow),
        new PropertyMetadata("Hello world"));

public string Text {
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}
我想将标签的内容绑定到该
Text
属性,以便标签显示
Text
属性的实际值,反之亦然

<Label Content="{Binding ???}" />

我怎么做


我以前做过,但现在我不记得怎么做了——这很简单。最简单的代码将被接受

必须设置窗口的DataContext才能使其工作。XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" 
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
      <StackPanel>
        <Label Content="{Binding Text}" />
        <Button Content="Click me" Click="HandleClick" />
      </StackPanel>

    </Grid>
</Window>

代码隐藏:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MainWindow), new PropertyMetadata("Hello world"));
    public string Text 
    { 
        get { return (string)GetValue(TextProperty); } 
        set { this.SetValue(TextProperty, value); } 
    }

    public MainWindow()
    {
        InitializeComponent();
    }

    protected void HandleClick(object sender, RoutedEventArgs e)
    {
        this.Text = "Hello, World";
    }
}
//
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
public static dependencProperty TextProperty=dependencProperty.Register(“文本”、typeof(字符串)、typeof(主窗口)、new PropertyMetadata(“Hello world”);
公共字符串文本
{ 
获取{return(string)GetValue(TextProperty);}
set{this.SetValue(TextProperty,value);}
}
公共主窗口()
{
初始化组件();
}
受保护的void HandleClick(对象发送器,RoutedEventArgs e)
{
this.Text=“你好,世界”;
}
}

将窗口/控件的DataContext设置为同一类,然后在绑定上指定路径,如下所示:

public class MyWindow : Window {

    public MyWindow() {
        InitializeComponents();
        DataContext = this;
    }

    public string Text { ... }    
}
然后在xaml中:

<Label Content="{Binding Path=Text}">

当你说它在代码背后,你的意思是它在你的类窗口的代码里


您可能希望绑定到祖先类型为Window的RelativeSource。或者,如果尚未设置数据上下文,则在加载事件中,将窗口的DataContext属性设置为窗口本身(如下所示),只需使用{Binding Text}。

在XAML中将DataContext设置为代码隐藏可能有点棘手,但通常这些情况是最常见的:

  • 您希望使整个窗口中的数据上下文成为窗口或 自定义用户控件
  • 
    

    
    
    二,。如果您将窗口或用户控件的DataContext设置为代码隐藏以外的内容,并且有一个子控件,则需要将其DataContext设置为代码隐藏,您可以使用以下方法:

    <Label DataContext={Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}/>
    
    
    
    对于自定义用户控件

    <Label DataContext={Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}/>
    
    
    

    在这种情况下,将DataContext设置为self,将使绑定引用标签对象本身,而不是控件的代码。我希望这会有所帮助。

    我已经尝试过这个:
    ,但它也不起作用。。。是的,代码隐藏意味着
    标签
    文本
    依赖属性处于同一类中。这里唯一的区别是
    标签
    在XAML中,依赖属性在代码中。谢谢。但是为什么它不在VS designer中显示
    标签
    s内容?!从代码隐藏设置DataContext时,blend不会显示绑定到数据上下文的数据。可以使用d:DataContext将设计时数据上下文设置为另一个对象,这有助于在混合中进行设计。请看这里:我不希望它混合在一起,而是在VisualStudio中。这是一样的吗?我试图将
    DataContext=“{Binding RelativeSource={RelativeSource Self}}”放入XAML代码中,但VS仍然没有在
    标签中显示绑定的数据。你也有同样的经历吗?或者您的VS在visual designer中显示绑定数据?如果您正在设置标签的DataContext属性,则基本上是将其设置为自身。标签恰好有一个文本属性,所以您可能不会看到任何数据绑定错误。您想将数据上下文设置为{RelativeSource FindAncestor,AncestorType={x:Type Window}}}或类似的内容。糟糕,忘记了数据上下文。现在试试。很抱歉(经过测试,效果很好。)+1更好,但在您可以编辑之前,其他人的速度更快。无论如何,感谢你提供了一个很好的例子,它对其他人来说是很有用的。我更喜欢这个方法,因为你不必看代码隐藏就能看到DataContext分配。但是如果你发现自己绑定了超过2到3件东西,那么你应该考虑把它们分割成一个视图模型!
    
    <Label DataContext={Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}/>
    
    <Label DataContext={Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}/>