C# WPF Listview绑定userControl属性

C# WPF Listview绑定userControl属性,c#,.net,wpf,C#,.net,Wpf,我是新来的WPF。我有Listview,我想在这里显示的不仅仅是usercontrol 我使用了ItemTemplate 类似这样的列表视图: <ListView HorizontalAlignment="Stretch" Margin="0,40,0,0" Name="listView1" VerticalAlignment="Stretch" ItemTemplate="{StaticResource DriveUserControlDataTemp

我是新来的
WPF
。我有
Listview
,我想在这里显示的不仅仅是
usercontrol

我使用了ItemTemplate

类似这样的列表视图:

    <ListView HorizontalAlignment="Stretch" Margin="0,40,0,0" Name="listView1" VerticalAlignment="Stretch"
              ItemTemplate="{StaticResource DriveUserControlDataTemplate}" >
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
                               ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
                               MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
                               ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>


    <DataTemplate x:Key="DriveUserControlDataTemplate" >
        <StackPanel Margin="10">
            <c:DriveUserControl Drive="{Binding Name}" EventAdd="DriveUserControlAdd_Click">
            </c:DriveUserControl >
        </StackPanel>
    </DataTemplate>
DriveInfo
的属性名为
Name


有人告诉我为什么它没有绑定吗?

WPF的主要实现方法是MVVM(Model-View-ViewModel),而不是代码隐藏。您需要做的是实现MVVM开发模式。我可以建议你先读一篇关于MVVM的在线文章吗

然而,我将尽力回答你的问题,而不是仅仅指向一个链接

创建一个类并将其命名为MainWindowViewModel,并实现INotifyPropertyChanged接口。完成此操作后,实现PropertyChanged事件。在这一点上,我喜欢作弊,并为自己编写一个helper方法,以便在属性更改时为我引发此事件

public class MainWindowViewModel : INotifyPropertyChanged {
  public event PropertyChangedEventHandler PropertyChanged;

  private void raisePropertyChanged(string propertyName) {
    if (PropertyChanged != null) PropretyChanged(this, new PropertyChangedEventArgs(propertyName));
  }
}
所以在这一点上,你应该有一些如上所示的东西。这是ViewModel

下一步是创建要向用户界面(视图)公开的属性。在您的情况下,您希望列出计算机上可用的驱动器。因此,我们需要创建字符串值的ObservableCollection,如下所示

private ObservableCollection<string> _drives = new ObservableCollection<string>();
public ObservableCollection<string> Drives {
  get { return _drives; }
  set {
    _drives = value;
    this.raisePropertyChanged("Drives");
  }
}
不要忘记用正确的值替换applicationNameSpace和applicationAssemblyName

接下来,我们在窗口的资源中声明ViewModel类

xmlns:vms="clr-namespace:applicationNameSpace;assembly=applicationAssemblyName"
<Window.Resources>
  <vms:MainWindowViewModel x:Key="mainWindowVms"/>
</Window.Resources>
所以这只剩下最后一件事要做,那就是回答您关于“为什么它没有绑定?”的问题,因为ItemsSource是一个需要由ViewModel中的属性绑定的属性。换句话说,它使用INotifyPropertyChanged接口通知UI线程已对其绑定到的对象进行了更改。代码隐藏中的绑定有点违背WPF的目的。因此,为了使绑定正常运行,只需在xaml中指定绑定即可

<ListView ItemsSource="{Binding Source={StaticResource mainWindowVms}, Path=Drives"/>

通过这种方式,您的视图模型会担心列表的状态和填充,而您的UI只会按照指示执行


好了。这一点很重要,特别是如果你是WPF新手,那么你应该仔细阅读MVVM模式开发,因为一旦你开始,就很难停止,你会想知道你以前做过什么。

没问题。我希望答案合适。如果这纠正了您的问题,请将其标记为已解决。
<Window.Resources>
  <vms:MainWindowViewModel x:Key="mainWindowVms"/>
</Window.Resources>
MainWindowViewModel mainWindowVms = Resources["mainWindowVms"] as MainWindowViewModel;
mainWindowVms.Init();
<ListView ItemsSource="{Binding Source={StaticResource mainWindowVms}, Path=Drives"/>