【发布时间】:2016-11-02 08:06:20
【问题描述】:
我实际上是在为自定义 ListView 创建一个 UserControl。 我想要达到的目标与这篇文章相同:Hide or Show stackpanel of ListViewItem with VisualStateManager。我已经实现了代码并且它可以工作,但现在我想让它更“通用”,所以我创建了一个用户控件。
我的控件分为两个网格。上层网格和折叠的隐藏网格。
我想像这样使用我的用户控件:
<controls:ExpandableListView ItemsSource="{Binding ConnectedObjects}">
<controls:ExpandableListView.Header>
<TextBlock Text="Hello World!" />
</controls:ExpandableListView.Header>
<controls:ExpandableListView.ExpandedContent>
<Button Content="test 2" />
</controls:ExpandableListView.ExpandedContent>
</controls:ExpandableListView>
Header 属性是要放置在上方网格中的内容,ExpandedContent 是放置在折叠网格中的内容。
在我的用户控件中,我有:
<ListView x:Name="LIST" SelectionChanged="LIST_SelectionChanged" ItemsSource="{Binding ItemsSource}">
<!-- Item container style -->
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<!-- Item template -->
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid x:Name="GRID_1" Grid.Row="0">
<ContentPresenter Content="{Binding Path=Header}" />
</Grid>
<Grid x:Name="GRID_2" Grid.Row="1" Visibility="Collapsed">
<ContentPresenter Content="{Binding Path=ExpandedContent}" />
</Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"></VisualState>
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Target="GRID_2.Visibility" Value="Visible"></Setter>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
用户控件后面有代码
public sealed partial class ExpandableListView : UserControl
{
private static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(
"ItemsSource",
typeof(IEnumerable),
typeof(ExpandableListView),
new PropertyMetadata(null));
public static readonly DependencyProperty HeaderContentProperty =
DependencyProperty.Register(
"Header",
typeof(Object),
typeof(ExpandableListView),
new PropertyMetadata(null));
public static readonly DependencyProperty ExpandedContentProperty =
DependencyProperty.Register(
"ExpandedContent",
typeof(Object),
typeof(ExpandableListView),
new PropertyMetadata(null));
public IEnumerable ItemsSource
{
get { return this.GetValue(ItemsSourceProperty) as IEnumerable; }
set { this.SetValue(ItemsSourceProperty, value); }
}
public Object Header
{
get { return (Object)this.GetValue(HeaderContentProperty); }
set { this.SetValue(HeaderContentProperty, value); }
}
public Object ExpandedContent
{
get { return (Object)this.GetValue(ExpandedContentProperty); }
set { this.SetValue(ExpandedContentProperty, value); }
}
public ExpandableListView()
{
this.InitializeComponent();
this.LIST.DataContext = this;
}
private void LIST_SelectionChanged(Object sender, SelectionChangedEventArgs e)
{
}
private void SetInEditMode()
{
VisualStateManager.GoToState(this, "Selected", true);
}
private void SetInViewMode()
{
VisualStateManager.GoToState(this, "Normal", true);
}
}
【问题讨论】:
-
你有什么问题?
-
我的问题是如何在我的 ListView 中显示我的
<controls:ExpandableListView.ExpandedContent>和<controls:ExpandableListView.Header>之间定义的内容。我正在使用 ItemTemplate 来格式化 Item 单元格,我有Header和ExpandedContent,它们是FrameworkElements。但它似乎不像那样工作。我也尝试过使用 ContentControl,但没有成功。
标签: c# .net xaml user-controls uwp