【问题标题】:How to bind DesignData to Single Property如何将 DesignData 绑定到单个属性
【发布时间】:2014-03-27 17:02:32
【问题描述】:

我正在尝试将我的 设计数据 绑定到 Grid 中的单个属性 (Margin)。我不想覆盖整个网格的数据上下文(这也是使用设计数据

<Grid 
  d:DataContext="{d:DesignData Source=/Blend/DesignData/HudDesignData.xaml}" 
  Margin="{Binding Source={d:DesignData Source=/Blend/DesignData/WPFSplitScreenLayoutDesignData.xaml}}">

设置为 DataContext 的设计数据按预期工作。

d:DataContext="{d:DesignData Source=/Blend/DesignData/HudDesignData.xaml}"

但是,我无法弄清楚如何将设计数据直接分配为Margin的Binding Source。

Margin="{Binding Source={d:DesignData Source=/Blend/DesignData/WPFSplitScreenLayoutDesignData}}"

上面的行给出了以下错误:

错误 1 ​​XML 命名空间中不存在标签“DesignData” 'http://schemas.microsoft.com/expression/blend/2008'。第 13 行位置 135.

谢谢!

【问题讨论】:

    标签: c# wpf datacontext design-data


    【解决方案1】:

    您只是缺少 wpfsplitscreenlayotudesigndata 中的 .xaml 吗?

    或者您将边距绑定到视图模型上的属性 然后从视图模型中找到您想要的 wpfsplitscreenlayoutdesigndata。

    Margin="{Binding Path = someint}"
    
    int someint
    {
        get
        { 
           //return the wpfspliscreenlayoutdesigndata which I assume you are getting from the visual tree
        }
    } 
    

    【讨论】:

    • 丢失的 .xaml 是一个错字。固定的。我认为您的建议行不通,因为设计数据的重点是在不影响运行时的情况下拥有“设计时间”特定数据。在您的示例中,您正在更改 Margin 的运行时绑定。
    • Margin="{Binding Source={d:DesignData Source=/Blend/DesignData/WPFSplitScreenLayoutDesignData.xaml}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"去
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    相关资源
    最近更新 更多