【问题标题】:How to bind by element name when control is inside a UserControl?当控件位于 UserControl 中时,如何按元素名称绑定?
【发布时间】:2014-08-19 19:08:05
【问题描述】:

我正在尝试使用元素名称将RectangleFill 属性绑定到页面DataContext 中的属性,但是因为RectangleUserControl 中,所以找不到页面所以不会发生绑定。

简化示例:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:Converters_Theme="using:ProjectName.Common.Converters"
    xmlns:CustomControls="using:ProjectName.CustomControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:ProjectName.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ViewModel="using:ProjectName.ViewModels"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ProjectName.Views.TestPage"
    x:Name="pageName"
    mc:Ignorable="d">

    <Page.Resources>
        <Converters_Theme:ThemeColorToSolidColorBrushConverter x:Key="ThemeColorToSolidColorBrushConverter"/>
        <DataTemplate x:Key="ItemDataTemplate">
            <StackPanel Orientation="Vertical">
                <!-- ... -->
                <Rectangle Fill="{Binding DataContext.Theme.Color, Converter={StaticResource ThemeColorToSolidColorBrushConverter}, ElementName=pageName}" MinHeight="40" MinWidth="40" />
                <!-- ... -->
            </StackPanel>
        </DataTemplate>
    </Page.Resources>

    <Page.DataContext>
        <ViewModel:PageViewModel />
    </Page.DataContext>

    <Grid>
        <CustomControls:Tab>
            <CustomControls:Tab.LeftContent>
                <ItemsControl ItemsSource="{Binding Items}" ItemTemplate="{StaticResource ItemDataTemplate}" />
            </CustomControls:Tab.LeftContent>
            <CustomControls:Tab.RightContent>
                <!-- ... -->
            </CustomControls:Tab.RightContent>
        </CustomControls:Tab>
    </Grid>
</Page>

【问题讨论】:

  • 我不确定我是否 100% 得到了你的问题,但你的 DataContext 对象是什么?
  • @Adil My DataContext 是一个视图模型,其中包含一个名为 Items 的属性和一个名为 Theme 的属性。
  • 基于“ElementName”的绑定在 winrt 应用程序中不再可用。您必须更改数据的结构方式,以将“颜色”的值存储在“项目”集合的每个对象中
  • @Miiite 您可以在ElementName 上进行绑定,当我对不在自定义用户控件内的事物使用绑定时,它可以正常工作。只是这种情况是行不通的。

标签: xaml data-binding windows-runtime winrt-xaml


【解决方案1】:

实现它的简单方法是使您的 ViewModel 成为静态资源,例如在 App.xaml 中(可能在您的 Page.xaml 中工作):

<App.Resources>
    <ViewModel:PageViewModel x:Key="MyPageViewModel" />
</App.Resources>

然后在您的 Page.xaml 中引用它:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:Converters_Theme="using:ProjectName.Common.Converters"
    xmlns:CustomControls="using:ProjectName.CustomControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:ProjectName.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ViewModel="using:ProjectName.ViewModels"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ProjectName.Views.TestPage"
    x:Name="pageName"
    DataContext="{StaticResource MyPageViewModel}"
    mc:Ignorable="d">

    <Page.Resources>
        <Converters_Theme:ThemeColorToSolidColorBrushConverter x:Key="ThemeColorToSolidColorBrushConverter"/>
        <DataTemplate x:Key="ItemDataTemplate">
            <StackPanel Orientation="Vertical">
                <!-- ... -->
                <Rectangle Fill="{Binding Theme.Color, Source={StaticResource MyPageViewModel}, Converter={StaticResource ThemeColorToSolidColorBrushConverter}}" MinHeight="40" MinWidth="40" />
                <!-- ... -->
            </StackPanel>
        </DataTemplate>
    </Page.Resources>

    <Grid>
        <CustomControls:Tab>
            <CustomControls:Tab.LeftContent>
                <ItemsControl ItemsSource="{Binding Items}" ItemTemplate="{StaticResource ItemDataTemplate}" />
            </CustomControls:Tab.LeftContent>
            <CustomControls:Tab.RightContent>
                <!-- ... -->
            </CustomControls:Tab.RightContent>
        </CustomControls:Tab>
    </Grid>
</Page>

要以更简洁的方式进行操作,我建议您将 ViewModelLocator 用作应用程序中的静态资源,它将属性到所有 ViewModel 并使其成为 App.xaml 中的 StaticResource。在 App.xaml 中实例化一个类比您拥有的每个 ViewModel 都更简洁。

【讨论】:

  • 虽然这听起来是个不错的选择,但静态要求让我无法使用它。
猜你喜欢
  • 2017-06-13
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 2011-06-29
  • 1970-01-01
相关资源
最近更新 更多