我在XAML中有一个控件模板,其中包含一个带有gridview的列表视图。我想将listview绑定到在C#中创建的数据表但我看不到listview这样做,因为它在控件模板中。我通过引用控件模板及其内容在C#中创建了等效的列表视图,并认为我可以使用它来设置绑定但是它没有工作。有没有人有任何建议?
<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>
代码隐藏
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);
}
答案 0 :(得分:0)
尝试使用
lvBonusHistory.Source = dtBonusHistory;
无论如何,这就是你想要实现的目标。
答案 1 :(得分:0)
在父控件上创建附加属性,然后在ControlTemplate中使用TemplateBinding。这样您就可以将源传递给ControlTemplate。