tags:

views:

1138

answers:

2

I have a control template in XAML that contains a listview with a gridview. I want to bind the listview to a datatable created within C# but I can’t see the listview to do this because it is within a control template. I’ve created the equivalent listview in C# by referencing the control template and its contents and thought that I would be able to use this to set the binding but it hasn’t worked. Does any one have any suggestions?

<ControlTemplate x:Key="HistoryView" TargetType="{x:Type ListViewItem}">

              <StackPanel x:Name="stkBonusHistory" Height="400" Visibility="Visible">

                <GridViewRowPresenter Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}"/>

                <!--this is the listview which I am hoping to turn into the "expander" detail-->
                    <ListView x:Name="lvBonusHistory" Width="950" Visibility="Visible" Height="300">
                      <ListView.View>
                        <GridView x:Name="gvBonusHistory" ColumnHeaderTemplate="{StaticResource GridViewHeader}" >
                        <GridViewColumn Header="Bonus Start" DisplayMemberBinding="{Binding BonusStart}" Width="150"/>
                        <GridViewColumn Header="Bonus End" DisplayMemberBinding="{Binding BonusEnd}" Width="150"/>
                        <GridViewColumn Header="Length" DisplayMemberBinding="{Binding Duration}" Width="75"/>
                        <GridViewColumn Header="Status" DisplayMemberBinding="{Binding BonusStatus}" Width="75"/>
                        <GridViewColumn Header="Note" DisplayMemberBinding="{Binding Note}" Width="75"/>
                      </GridView>
                    </ListView.View>
                  </ListView>
                </StackPanel>
            </ControlTemplate>

          <ListView x:Name="lvBonusList" Width="1000">
            <ListView.ItemContainerStyle>
              <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Template" Value="{StaticResource Default}"/>
                <Style.Triggers>
                  <DataTrigger Binding="{Binding LeaversBonus}" Value="1">
                    <Setter Property="Template" Value="{StaticResource LeaversBonus}"/>
                  </DataTrigger>
                  <DataTrigger Binding="{Binding HistoryShow}" Value="1">
                    <Setter Property="Template" Value="{StaticResource Default}"/>
                    <Setter Property="Template" Value="{StaticResource HistoryView}"/>
                  </DataTrigger>
                </Style.Triggers>
              </Style>
            </ListView.ItemContainerStyle>

            <ListView.View>
              <GridView x:Name="gvBonusList" ColumnHeaderTemplate="{StaticResource GridViewHeader}" >
                <GridViewColumn x:Name="BonusHistory" Width="20" CellTemplate="{StaticResource BonusHistoryGraphic}" >
                </GridViewColumn>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding EmployeeName}" Width="250"/>
                <GridViewColumn Header="Emp ID" DisplayMemberBinding="{Binding EmpID}" Width="75"/>
                <GridViewColumn Header="Employee Start" DisplayMemberBinding="{Binding EmpStart}" Width="150" />
                <GridViewColumn Header="Employee End" DisplayMemberBinding="{Binding EmpEnd}" Width="150"/>
                <GridViewColumn Header="Bonus Start" DisplayMemberBinding="{Binding BonusStart}" Width="150"/>
                <GridViewColumn Header="Bonus End" DisplayMemberBinding="{Binding BonusEnd}" Width="150"/>
                <GridViewColumn Header="Length" DisplayMemberBinding="{Binding Duration}" Width="75"/>
              </GridView>
            </ListView.View>
          </ListView>

Codebehind

private void PopulateBonusHistory()
{
        // populates dtBonusHistory
        BonusHistoryGet();
        ControlTemplate HistoryView = (ControlTemplate)stkBonusList.Resources["HistoryView"];
        StackPanel stkBonusHistory = (StackPanel)HistoryView.LoadContent();
        (stkBonusHistory.Children[1]).Visibility = Visibility.Collapsed;
        ListView lvBonusHistory = (ListView)stkBonusHistory.Children[1];
        lvBonusHistory.View = (GridView)(lvBonusHistory.FindName("gvBonusHistory"));

        Binding bind = new Binding();
        lvBonusHistory.DataContext = dtBonusHistory;
        lvBonusHistory.SetBinding(ListView.ItemsSourceProperty, bind);
    }
A: 

try using

lvBonusHistory.Source = dtBonusHistory;

as thats what you are trying to acheive anyway.

Aran Mulholland
A: 

Create an attached property on the parent control then use TemplateBinding inside the ControlTemplate. This way you can pass the source into ControlTemplate.

Kai Wang