ContentControl 中的绑定崩溃

发布于 2024-10-09 02:30:28 字数 635 浏览 0 评论 0原文

谁能告诉我为什么这会使我的应用程序崩溃?似乎有一些无休止的递归,我不明白为什么。我得到这个例外

逻辑树深度超出范围 遍历树。这可以 表示树中的一个循环

<ContentControl Content="{Binding}">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Button Content="{Binding MyString}"/>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

这就是我作为源的全部内容

    public MainWindow()
    {
        InitializeComponent();
        MyString = "Test";
        this.DataContext = this;
    }

    public string MyString { get; set; }

Can anyone tell me why this crashes my app? There seems to be some endless recursion by I can't figure out why. I get this exception

Logical tree depth exceeded while
traversing the tree. This could
indicate a cycle in the tree

<ContentControl Content="{Binding}">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Button Content="{Binding MyString}"/>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

And this is all I have as Source

    public MainWindow()
    {
        InitializeComponent();
        MyString = "Test";
        this.DataContext = this;
    }

    public string MyString { get; set; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两仪 2024-10-16 02:30:28

您使用 MainWindow 作为 MainWindow 内容的 DataContext。当您在 ContentControl 上设置 Content="{Binding}" 时,这会将 ContentControl 的内容设置为 MainWindow 实例。这是一个问题,因为 ContentControl 包含在 MainWindow 的 Content 中。每当 Content 属性接收 UIElement 时,它都会将其呈现为 UIElement,而不是像非 UI 类那样通过 DataTemplate。因此,您的树最终会

MainWindow
 ContentControl
  MainWindow
   ContentControl
    ...

为您的 DataContext 使用单独的数据对象而不是窗口本身将为您提供您正在寻找的行为:

public partial class Window13 : Window
{
    public Window13()
    {
        InitializeComponent();
        MyData data = new MyData();
        data.MyString = "Test";
        this.DataContext = data;
    }
}

public class MyData
{
    public string MyString { get; set; }
}

You're using MainWindow as the DataContext of the MainWindow's content. When you set Content="{Binding}" on the ContentControl this is setting the ContentControl's content to the MainWindow instance. This is a problem because the ContentControl is contained in the MainWindow's Content. Whenever a Content property receives a UIElement, it renders it as a UIElement, not through the DataTemplate as it would with an non-UI class. So your tree ends up being

MainWindow
 ContentControl
  MainWindow
   ContentControl
    ...

Using a separate data object for your DataContext instead of the window itself will give you the behavior you're looking for:

public partial class Window13 : Window
{
    public Window13()
    {
        InitializeComponent();
        MyData data = new MyData();
        data.MyString = "Test";
        this.DataContext = data;
    }
}

public class MyData
{
    public string MyString { get; set; }
}
胡大本事 2024-10-16 02:30:28

尽管我完全同意您不应该这样做的公认答案,但有时您只是没有选择。例如,我正在使用 Xceed PropertyGridDataContext我为网格的每个项目都有一个 PropertyItem ,它是一个 UIElement (包含 Value 成员中的实际数据)。

我发现的解决方法是使用 ContentPresenter 而不是 ContentControl。文档对此并不清楚,但似乎 UIElement 是模板化的,而不是按原样使用。

<ContentPresenter Content="{Binding}">
    <ContentPresenter.ContentTemplate>
        <DataTemplate>
            <Button Content="{Binding MyString}"/>
        </DataTemplate>
    </ContentPresenter.ContentTemplate>
</ContentPresenter>

Although I totally agree with the accepted answer that you should not do this, sometimes you just don't have the choice. For instance, I'm using Xceed PropertyGrid and the DataContext I have for each item of the grid is a PropertyItem which is a UIElement (containing the actual data in a Value member).

The workaround I found is to use a ContentPresenter instead of a ContentControl. The documentation is not clear about this but it seems that UIElement are templated instead of being used as-is.

<ContentPresenter Content="{Binding}">
    <ContentPresenter.ContentTemplate>
        <DataTemplate>
            <Button Content="{Binding MyString}"/>
        </DataTemplate>
    </ContentPresenter.ContentTemplate>
</ContentPresenter>
岁月流歌 2024-10-16 02:30:28

您应该删除 ContentControl 的 Content 属性上的绑定。无论如何,这应该做什么?

You should remove the binding on the Content property of the ContentControl. What is this supposed to do anyway?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文