【问题标题】:Databinding to a ViewBase数据绑定到 ViewBase
【发布时间】:2011-05-08 18:58:06
【问题描述】:

我正在尝试使用 ViewBase 在单击按钮时将一些数据绑定到 ListView。它似乎不起作用,我不知道我做错了什么。下面是反映这个问题的一个简短的独立程序:

主窗口:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2;assembly="
    Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <DataTemplate x:Key="ListViewItemTemplate" DataType="ListViewItem">
      <Border Name="LastBorder">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <Rectangle Height="{Binding Height}" Width="{Binding Width}" Fill="{Binding Fill}"/>
        </Grid>
      </Border>
    </DataTemplate>
    <Style TargetType="ListViewItem" x:Key="ItemContainerStyle1">
      <Setter Property="VerticalAlignment" Value="Top" />
    </Style>
    <ObjectDataProvider x:Key="ShapeProvider" />


  </Window.Resources>
  <Grid>
    <Button Content="Bind data" Click="Button_Click" Height="20" Width="80" Margin="12,12,410,278" />
    <ListView ItemsSource="{Binding Path=.,Source={StaticResource ShapeProvider}}"
         ItemContainerStyle="{StaticResource ItemContainerStyle1}" x:Name="ListView1" Height="113" VerticalAlignment="Bottom">
      <ListView.View>
        <local:PlainView ItemWidth="148" ItemHeight="250"
                 ItemTemplate="{StaticResource ListViewItemTemplate}" />
      </ListView.View>
    </ListView>
  </Grid>
</Window>

我的视图库:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication2
{
  public class PlainView : ViewBase
  {
    public static readonly DependencyProperty ItemContainerStyleProperty =
      ItemsControl.ItemContainerStyleProperty.AddOwner(typeof(PlainView));

    public Style ItemContainerStyle
    {
      get { return (Style)GetValue(ItemContainerStyleProperty); }
      set { SetValue(ItemContainerStyleProperty, value); }
    }

    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); }
    }

    protected override object DefaultStyleKey
    {
      get
      {
        ComponentResourceKey key = new ComponentResourceKey(GetType(), "PlainViewRsx");
        return key;
      }
    }

    public PlainView()
    {
      ItemWidth = 40;
      ItemHeight = 20;
    }
  }
}

视图风格:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfApplication2">
  <!--PlainView Default Style for ListView-->
  <Style x:Key="{ComponentResourceKey 
    TypeInTargetAssembly={x:Type local:PlainView},
    ResourceId=PlainViewRsx}" 
    TargetType="{x:Type ListView}" 
    BasedOn="{StaticResource {x:Type ListBox}}">

    <!--
    Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
    -->

    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="ItemContainerStyle" 
      Value="{Binding (ListView.View).ItemContainerStyle, RelativeSource={RelativeSource Self}}" />
    <Setter Property="ItemTemplate" 
      Value="{Binding (ListView.View).ItemTemplate, RelativeSource={RelativeSource Self}}" />
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <WrapPanel HorizontalAlignment="Stretch"
            Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
            ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
            MinWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
            ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  </ResourceDictionary>

还有我正在使用的对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Media;

namespace WpfApplication2
{
  public class RectShapes : INotifyPropertyChanged
  {
    public int Height { get; set; }
    public int Width { get; set; }
    public SolidColorBrush Fill { get; set; }

    public RectShapes()
    {
      Width = 98;
      Height = 152;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
        handler(this, new PropertyChangedEventArgs(name));
      }
    }
  }
}

我使用 snoop 检查是否存在绑定错误,但似乎列表视图甚至没有子视图,因此即使我在 XAML 中有设置,也不会以某种方式添加 PlainView。

谢谢!

【问题讨论】:

    标签: c# wpf data-binding listview binding


    【解决方案1】:

    我不认为这个观点有什么问题。

    我认为问题可以在这里找到:

    <ListView ItemsSource="{Binding Path=.,Source={StaticResource ShapeProvider}}"
         ItemContainerStyle="{StaticResource ItemContainerStyle1}" x:Name="ListView1" Height="113" VerticalAlignment="Bottom">
    

    试试这个:

    <ListView ItemsSource="{Binding Source={StaticResource ShapeProvider}}"
         ItemContainerStyle="{StaticResource ItemContainerStyle1}" x:Name="ListView1" Height="113" VerticalAlignment="Bottom">
    

    或者只是为了排除故障,删除视图并运行您的代码,如果列表视图不显示任何内容,则问题出在绑定上。

    【讨论】:

    • 摆脱路径没有帮助。摆脱视图有效(因为它显示了形状),但它只是垂直线上的形状,我想使用 ViewBase 来使用包装面板。
    【解决方案2】:

    好的,我明白了:D

    我目前正在从事一个也需要普通视图的项目,我就是这样做的:

    <Window.Resources>
          <DataTemplate x:Key="iconTemplate">
            <DockPanel Height="40" Width="150">
                <Image Margin="6" Height="30" Width="30" 
                       Stretch="Uniform"
                       Source={Binding Pic}>
                </Image>
                <TextBlock DockPanel.Dock="Top" Text="{Binding Title}"
                           FontSize="13" HorizontalAlignment="Left"
                           Margin="0,0,0,3"/>
                <TextBlock Text="{Binding Artist}" FontSize="9"
                           HorizontalAlignment="Left" Margin="0,0,0,3"/>
            </DockPanel>
        <my:PlainView x:Key="iconView" 
            ItemTemplate="{StaticResource iconTemplate}" 
            ItemWidth="300"/>
    </Window.Resources>
    

    我的 plainview 使用的代码和你的差不多,这个标记很容易包装我的图标,所以我认为它可以帮助你

    【讨论】:

    • 这对您有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    相关资源
    最近更新 更多