定义的C后面的对象

定义的C后面的对象

本文介绍了绑定在$ C $定义的C后面的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被实例化在code背后的一些对象,例如,XAML中被称为window.xaml和window.xaml.cs内

I have some object that is instantiated in code behind, for instance, the XAML is called window.xaml and within the window.xaml.cs

protected Dictionary<string, myClass> myDictionary;

我怎么能这样对象绑定到,例如,一个列表视图,只使用XAML标记?

How can I bind this object to, for example, a list view, using only XAML markups?

更新:

(这正是我在我的测试code):

(This is exactly I have in my test code):

<Window x:Class="QuizBee.Host.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="{Binding windowname}" Height="300" Width="300"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
    </Grid>
</Window>

而在codebehind

And in codebehind

public partial class Window1 : Window
{
    public const string windowname = "ABCDEFG";

    public Window1()
    {
        InitializeComponent();
    }
}

假设标题应该成为ABCDEFG吧?但它结束了深藏不露。

Suppose the title should become "ABCDEFG" right? but it ends up showing nothing.

推荐答案

您可以设置的DataContext你的控制,表单等,像这样:

You can set the DataContext for your control, form, etc. like so:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

澄清

数据上下文被设置为值以上应不惜一切元素拥有的背后code进行 - 这样一个窗口,你应该设置在窗口申报

The data context being set to the value above should be done at whatever element "owns" the code behind -- so for a Window, you should set it in the Window declaration.

我有你的榜样与此code工作:

I have your example working with this code:

<Window x:Class="MyClass"
  Title="{Binding windowname}"
  DataContext="{Binding RelativeSource={RelativeSource Self}}"
  Height="470" Width="626">

在DataContext定在这个水平,那么通过在窗口中的任何元素(除非你明确地改变它的子元素)继承,因此设置的DataContext的窗口之后,你应该能够只是做直接绑定到codeBehind从窗口上的任何控件的属性。

The DataContext set at this level then is inherited by any element in the window (unless you explicitly change it for a child element), so after setting the DataContext for the Window you should be able to just do straight binding to CodeBehind properties from any control on the window.

这篇关于绑定在$ C $定义的C后面的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 07:30