【问题标题】:Databind from XAML to code behind从 XAML 到代码背后的数据绑定
【发布时间】:2011-08-15 00:57:48
【问题描述】:

我在后面的代码中有这个 Text 依赖属性:

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 ???}" />

我该怎么做?

我之前做过一段时间,但现在我不记得怎么做了——而且很简单。最简单的代码将被接受。

【问题讨论】:

  • 我试过这个:&lt;Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfApplication1:MainWindow}}, Path=Text}" /&gt;,但它也不起作用......难道绑定在其他一切都有效的全新 WPF 项目中不起作用?

标签: c# .net wpf data-binding binding


【解决方案1】:

将 XAML 中的 DataContext 设置为 Code-Behind 可能有点棘手,但通常这些情况是最常见的:

  1. 您想让 DataContext 成为整个 Window 或 自定义用户控件

.

<Window
blahhhh..
DataContext={Binding RelativeSource={RelativeSource Mode=Self}}>

<UserControl
Blahhhh....
DataContext={Binding RelativeSource={RelativeSource Mode=Self}}>

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

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

对于自定义 UserControl

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

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

【讨论】:

    【解决方案2】:

    您必须设置窗口的 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";
        }
    }
    

    【讨论】:

    • 我的错,忘记了 DataContext。现在试试。对不起。 (经过测试,它可以工作。)
    • +1 更好,但其他人更快,在您可以编辑之前。无论如何,谢谢你的好例子,它对其他人有用。
    • 我更喜欢这种方法,因为您不必查看代码隐藏即可查看 DataContext 分配。但是,如果你发现自己将超过 2 或 3 个东西绑定到代码隐藏中,你真的应该考虑将它们拆分成一个 ViewModel!
    【解决方案3】:

    当您说它在代码隐藏中时,您的意思是它在您班级的 Window 的代码中?

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

    【讨论】:

    • yes 代码隐藏意味着LabelText 依赖属性属于同一类。这里唯一的区别是Label 在 XAML 中,而在代码中的依赖属性。
    【解决方案4】:

    将 Window/Control 的 DataContext 设置为同一个类,然后在绑定上指定路径,如下所示:

    public class MyWindow : Window { public MyWindow() { InitializeComponents(); DataContext = this; } public string Text { ... } }

    然后在你的 xaml 中:

    &lt;Label Content="{Binding Path=Text}"&gt;

    【讨论】:

    • 谢谢。但是为什么它没有在 VS 设计器中显示 Labels 内容?!
    • 从代码隐藏设置 DataContext 时,混合不会显示绑定到数据上下文的数据。您可以使用 d:DataContext 将设计时数据上下文设置为另一个便于混合设计的对象。见这里:stackoverflow.com/questions/862829/…
    • 我不想在 Blend 中而是在 VisualStudio 中。是一样的吗?
    • 我尝试将DataContext="{Binding RelativeSource={RelativeSource Self}}" 放入XAML 代码,但VS 仍然不显示Label 中的绑定数据。你有同样的想法吗?还是你的 VS 在可视化设计器中显示绑定数据?
    • 如果您正在设置标签的 DataContext 属性,那么您基本上是在将其设置为自身。 Label 恰好有一个 Text 属性,因此您可能不会看到任何数据绑定错误。您想将数据上下文设置为 {RelativeSource FindAncestor,AncestorType={x:Type Window}} 或类似的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多