3

I have a gridView, and binding my ObservableCollection different class items for my hub page. Binding different class items to gridView and showing different item Data Templates, Header Templates.

I using User Control in Header Template in gridview. My headertemplate, including user control and user control include some comboboxes. I want bind in pageRoot datacontext collection to in headertemplate combobox via parameter by on pageRoot object.

I'm create a user control and create some DependencyProperty, combobox eventhandlers. But can't bind pageRoot DataContext collection to in user control comboboxes :(

My english is poor, sorry for this situation ;) Thanks for your answers..

My headerdatatemplate:

<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>

My user control code:

<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>

User control code behind:

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.
        }
}
Qortex
  • 7,087
  • 3
  • 42
  • 59
The Goat
  • 1,080
  • 13
  • 19

1 Answers1

0

I'm resolved my code problems, and some code added.

1- Bind user control of root page DataContext. Because user control inside gridview template. If insided a control in datatemplate, binding default datacontext of datatemplate itemsources. I want don't related with itemsource datacontext, bind rootpage datacontext. Replaced Codes, in 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- I'm replace in user control xaml code. User Control's Datacontext is of rootPage DataContext now. I'm can be binding every collection to my comboboxes this situation. Replaced codes:

<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>

Thanks for thinking on this problem. This structure can be communicated every element, and can be binding every collection without datatemplate itemsource datacontext. If use a datatemplate and need eventhandlers of controls, can be use User control.

Good coded days ;)

Qortex
  • 7,087
  • 3
  • 42
  • 59
The Goat
  • 1,080
  • 13
  • 19