WPF中,ListView中数据显示的问题,部分代码如下:
在<window.Resources>中定义的<GridViewx:Key="GridView"><GridViewColumnWidth="150"Header="称"...
在<window.Resources>中定义的
<GridView x:Key="GridView"> <GridViewColumn Width="150" Header="称" CellTemplate="{StaticResource DisplayImage}">
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding DateTime}" Width="160" Header="修改时间">
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Type}" Width="90" Header="类型">
</GridViewColumn> <GridViewColumn DisplayMemberBinding="{Binding Size}" Width="110" Header="大小">
</GridViewColumn>
</GridView>
<DataTemplate x:Key="bigImage">
<DockPanel Height="33" Width="150">
<Image Source="{Binding Image}" Margin="2"></Image>
<TextBlock DockPanel.Dock="Top" Text="{Binding Name}" FontSize="13" HorizontalAlignment="Left" Margin="0,0,0,1" />
<TextBlock Text="{Binding Type}" FontSize="9" HorizontalAlignment="Left" Margin="0,0,0,1" />
</DockPanel>
</DataTemplate>
<loc:PlainView x:Key="ImageView" ItemTemplate="{StaticResource bigImage}" ItemWidth="100" ></loc:PlainView> 其中PlainView是自定义的类,继承自ViewBase,该类的代码如下:
class PlainView : ViewBase {
public PlainView()
{
} public static readonly DependencyProperty
ItemContainerStyleProperty =
ItemsControl.ItemContainerStyleProperty.AddOwner(typeof(PlainView));
public static readonly DependencyProperty ItemTemplateProperty = ItemsControl.ItemTemplateProperty.AddOwner(typeof(PlainView)); public DataTemplate ItemTemplate {
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public static readonly DependencyProperty ItemWidthProperty = WrapPanel.ItemWidthProperty.AddOwner(typeof(PlainView));
public double ItemWidth {
get { return (double)GetValue(ItemWidthProperty); }
set { SetValue(ItemWidthProperty, value); }
}
public static readonly DependencyProperty ItemHeightProperty =
WrapPanel.ItemHeightProperty.AddOwner(typeof(PlainView));
public double ItemHeight {
get { return (double)GetValue(ItemHeightProperty); }
set { SetValue(ItemHeightProperty, value); }
}
}
事件处理的部分代码是:
if (str == "GridView")
{
fileBox.View = fileBox.FindResource("GridView") as ViewBase;
}
else if (str == "ImageView")
{
fileBox.View = fileBox.FindResource("ImageView") as ViewBase;
}
运行后,资源GridView好用,但是ImageView不好用,显示的都是默认的数据。 展开
<GridView x:Key="GridView"> <GridViewColumn Width="150" Header="称" CellTemplate="{StaticResource DisplayImage}">
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding DateTime}" Width="160" Header="修改时间">
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Type}" Width="90" Header="类型">
</GridViewColumn> <GridViewColumn DisplayMemberBinding="{Binding Size}" Width="110" Header="大小">
</GridViewColumn>
</GridView>
<DataTemplate x:Key="bigImage">
<DockPanel Height="33" Width="150">
<Image Source="{Binding Image}" Margin="2"></Image>
<TextBlock DockPanel.Dock="Top" Text="{Binding Name}" FontSize="13" HorizontalAlignment="Left" Margin="0,0,0,1" />
<TextBlock Text="{Binding Type}" FontSize="9" HorizontalAlignment="Left" Margin="0,0,0,1" />
</DockPanel>
</DataTemplate>
<loc:PlainView x:Key="ImageView" ItemTemplate="{StaticResource bigImage}" ItemWidth="100" ></loc:PlainView> 其中PlainView是自定义的类,继承自ViewBase,该类的代码如下:
class PlainView : ViewBase {
public PlainView()
{
} public static readonly DependencyProperty
ItemContainerStyleProperty =
ItemsControl.ItemContainerStyleProperty.AddOwner(typeof(PlainView));
public static readonly DependencyProperty ItemTemplateProperty = ItemsControl.ItemTemplateProperty.AddOwner(typeof(PlainView)); public DataTemplate ItemTemplate {
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public static readonly DependencyProperty ItemWidthProperty = WrapPanel.ItemWidthProperty.AddOwner(typeof(PlainView));
public double ItemWidth {
get { return (double)GetValue(ItemWidthProperty); }
set { SetValue(ItemWidthProperty, value); }
}
public static readonly DependencyProperty ItemHeightProperty =
WrapPanel.ItemHeightProperty.AddOwner(typeof(PlainView));
public double ItemHeight {
get { return (double)GetValue(ItemHeightProperty); }
set { SetValue(ItemHeightProperty, value); }
}
}
事件处理的部分代码是:
if (str == "GridView")
{
fileBox.View = fileBox.FindResource("GridView") as ViewBase;
}
else if (str == "ImageView")
{
fileBox.View = fileBox.FindResource("ImageView") as ViewBase;
}
运行后,资源GridView好用,但是ImageView不好用,显示的都是默认的数据。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询