3

我有一个 gridView,并为我的中心页面绑定了我的 ObservableCollection 不同的类项。将不同的类项绑定到gridView,并显示不同的项数据模板、表头模板。

我在gridview的标题模板中使用用户控件。我的标题模板,包括用户控件和用户控件,包括一些组合框。我想通过pageRoot对象上的参数将pageRoot datacontext集合绑定到headertemplate组合框中。

我正在创建一个用户控件并创建一些 DependencyProperty、组合框事件处理程序。但无法将 pageRoot DataContext 集合绑定到用户控件组合框中:(

我的英语很差,对这种情况感到抱歉;)感谢您的回答..

我的标题数据模板:

<DataTemplate x:Key="OrganizationFixtureHeaderTemplate">
    <Grid Margin="6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0">
            <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}"/>
        </StackPanel>

        <UserControls:UcCbOrganizationFixtureSelections  
            Grid.Column="1"
            RootElement="pageRoot"
            PageOrganizationSource="{Binding PageOrganization, ElementName=pageRoot}"
            />

    </Grid>
</DataTemplate>

我的用户控制代码:

<UserControl
    x:Class="Modern_UI.Common.UserControls.UcCbOrganizationFixtureSelections"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Modern_UI.Common.UserControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal">
        <ComboBox x:Name="cbOrgFixtureSeasons" 
                          ItemsSource="{Binding Path=DataContext.Seasons, ElementName=RootElement}"
                          SelectedValuePath="Id" 
                          DisplayMemberPath="Name"
                          SelectionChanged="CbOrgFixtureSeasons_OnSelectionChanged"
                          Width="Auto"
                          Height="Auto"
                         />
        <ComboBox x:Name="cbOrgFixtureStages" 
                          ItemsSource="{Binding Path=DataContext.FixtureStages, ElementName=RootElement}"
                          SelectedValuePath="Id" 
                          DisplayMemberPath="Name"
                          SelectionChanged="CbOrgFixtureStages_OnSelectionChanged"
                          Width="Auto"
                          Height="Auto"
                  />
        <ComboBox x:Name="cbOrgFixtureRounds" 
                     ItemsSource="{Binding Path=DataContext.FixtureRounds, ElementName=RootElement}"
                     SelectedValuePath="Id" 
                     DisplayMemberPath="Name"
                     SelectionChanged="CbOrgFixtureRounds_OnSelectionChanged"
                     Width="Auto"
                     Height="Auto"
                  />
    </StackPanel>
</UserControl>

后面的用户控制代码:

namespace Modern_UI.Common.UserControls
{
    public sealed partial class UcCbOrganizationFixtureSelections : UserControl
    {  
        public DependencyProperty RootElementProperty = DependencyProperty.Register("RootElement",
                                                                                    typeof(string),
                                                                                    typeof(
                                                                                        UcCbOrganizationFixtureSelections
                                                                                        ),
                                                                                    null);


        public DependencyProperty PageOrganizationSourceProperty = DependencyProperty.Register("PageOrganizationSource",
                                                                                               typeof(Organization),
                                                                                               typeof(
                                                                                                   UcCbOrganizationFixtureSelections
                                                                                                   ),
                                                                                               null);

        public string RootElement
        {
            get { return this.GetValue(RootElementProperty) as string; }
            set { this.SetValue(RootElementProperty, value); }
        }

        public Organization PageOrganizationSource
        {
            get { return this.GetValue(PageOrganizationSourceProperty) as Organization; }
            set { this.SetValue(PageOrganizationSourceProperty, value); }
        }


        public UcCbOrganizationFixtureSelections()
        {
            this.InitializeComponent();
        }

        async private void CbOrgFixtureSeasons_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          //loading root page parameter via "PageOrganizationSource" named DependencyProperty.
        }
}
4

1 回答 1

0

我解决了我的代码问题,并添加了一些代码。

1-绑定根页面DataContext的用户控件。因为gridview模板里面的用户控制。如果在数据模板中的控件内,则绑定数据模板项目源的默认数据上下文。我不想与 itemsource 数据上下文相关,绑定 rootpage 数据上下文。替换代码,在 DataTemplate 中:

<DataTemplate x:Key="OrganizationFixtureHeaderTemplate">
    <Grid Margin="6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0">
            <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}"/>
        </StackPanel>

        <UserControls:UcCbOrganizationFixtureSelections  
            Grid.Column="1"
            RootElement="pageRoot"
            DataContext="{Binding DataContext, ElementName=pageRoot}"     
            PageOrganizationSource="{Binding PageOrganization, ElementName=pageRoot}"
            />
    </Grid>
</DataTemplate> 

2-我在用户控件 xaml 代码中替换。用户控件的 Datacontext 现在属于 rootPage DataContext。在这种情况下,我可以将每个集合绑定到我的组合框。替换代码:

<UserControl
    x:Class="Lig_TV_Modern_UI.Common.UserControls.UcCbOrganizationFixtureSelections"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal">
        <ComboBox x:Name="cbOrgFixtureSeasons" 
                          ItemsSource="{Binding Seasons}"
                          SelectedValuePath="Id" 
                          DisplayMemberPath="Name"
                          SelectionChanged="CbOrgFixtureSeasons_OnSelectionChanged"
                          Width="Auto"
                          Height="Auto"                             
                         />        
    </StackPanel>
</UserControl>

感谢您思考这个问题。这种结构可以与每个元素进行通信,并且可以绑定每个集合而无需 datatemplate itemsource datacontext。如果使用数据模板并且需要控件的事件处理程序,可以使用用户控件。

编码良好的日子;)

于 2012-12-03T13:37:35.987 回答