繁体   English   中英

绑定到UserControl内部的ListView的ItemsSource和SelectedValue

[英]Bind to ItemsSource and SelectedValue of a ListView inside of a UserControl

我的目标是重用ListView和我设计的UserControl内部的其他几个控件。

为了简洁起见,请想象我有一个像这样的Person类,以及它的实例列表。

public class Person
{
    public string Name { get; set; }
    public string City { get; set; }
}

我的MainWindow

<Window x:Class="ReusableListView.MainWindow"
        ...
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="600" Width="600">
    <Grid>        
        <local:UCListView Margin="8"
                          ItemsSource="{Binding PersonList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private ObservableCollection<Person> _personList = null;
    public ObservableCollection<Person> PersonList
    {
        get { return _personList; }
        set { _personList = value; OnPropertyChanged("PersonList"); }
    }

    private Person _selectedPerson = null;
    public Person SelectedPerson
    {
        get { return _selectedPerson; }
        set { _selectedPerson = value; OnPropertyChanged("SelectedPerson"); }
    }

    public MainWindow()
    {
        InitializeComponent();
        PersonList = GetPeople();
    }

    private ObservableCollection<Person> GetPeople()
    {
        var list = new ObservableCollection<Person>
        {
            new Person() { Name = "Jane", City = "NYC" },
            new Person() { Name = "John", City = "LA" }
        };
        return list;
    }
}

我想将PersonName属性显示为UserControl ListView单个项目,然后在它的右侧显示想要的人的City属性。 所以我的UserControl看起来像这样:

<UserControl x:Class="ReusableListView.UCListView"
             ...
             x:Name="MyListViewUC"
             d:DesignHeight="500" d:DesignWidth="580">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListView Grid.Column="0" MinWidth="256" Margin="8"
                  DataContext="{Binding ElementName=MyListViewUC}"
                  ItemsSource="{Binding ItemsSource}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"
                               Width="Auto" Margin="8" Background="Pink"
                               Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <TextBox Grid.Column="1" Margin="8" Background="PaleGreen"/>
    </Grid>
</UserControl>

以及后面的UserControl代码:

public partial class UCListView : UserControl
{
    public UCListView()
    {
        InitializeComponent();
    }

    public object ItemsSource
    {
        get { return GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(UCListView), new PropertyMetadata(null));
}

上面的代码是根据我在网上看到的大多数示例(包括SO)缝合在一起的。 这是我的问题和疑问。

  1. 当我运行此命令时, UserControl列表中什么也不显示。 似乎是什么问题?
  2. 如何将SelectedPerson属性绑定到UserContro. 因此它知道如何根据选择显示正确的City

所以这个让我感兴趣。 我弄乱了代码,发现为了完成这项工作,我必须按照Mark的建议为MainWindow设置DataContext。 因此,在MainWindow构造函数中,您只需将

DataContext = this;

我还发现,您的依赖项属性设置方式存在问题。 您已将其设置为对象。 如果将其设置为IEnumerable,它将起作用。 我敢肯定,有一种更通用的方法可以做到这一点,但是,这应该使您走上正确的道路。 问题是ItemsSource无法使用对象。 它需要IEnumerable。

public IEnumerable ItemsSource
{
    get { return (IEnumerable)GetValue(ItemsSourceProperty); }
    set { SetValue(ItemsSourceProperty, value); }
}

public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
    nameof(ItemsSource), typeof(IEnumerable), typeof(UCListView));

您需要做的最后一件事是创建一个依赖对象以通过DisplayMemberPath传递,或者在用户控件中静态设置它。 我只是静态地设置了它,但是您可能想要创建一个依赖属性来传递它,以便它可以是动态的。

<ListView Grid.Column="0" MinWidth="256" Margin="8"
          x:Name="listView"
          DataContext="{Binding ElementName=MyListViewUC}"
          DisplayMemberPath="Name"
          ItemsSource="{Binding ItemsSource}"/>

您将必须删除ItemTemplate。 我希望这是有帮助的!

除此之外,您错过了设置Window的DataContext

DataContext = this;

您应该考虑直接从ListView或更简单的ListBox派生您的控件,因为那样您就可以直接访问其所有有用的属性。

与UserControl的不同之处在于,XAML是ResourceDictionary Themes/Generic.xaml中的默认样式,当您将自定义控件添加到WPF项目时会自动生成该样式。

控件的代码,将基类从Control更改为ListBox:

public class MyListBox : ListBox
{
    static MyListBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(MyListBox),
            new FrameworkPropertyMetadata(typeof(MyListBox)));
    }
}

Generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:...">

    <Style TargetType="local:MyListBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:MyListBox">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>

                            <ScrollViewer Grid.Column="0">
                                <ItemsPresenter/>
                            </ScrollViewer>

                            <TextBlock Grid.Column="1"
                                       Text="{TemplateBinding SelectedValue}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

您将像其他任何ListBox一样使用MyListBox:

<local:MyListBox ItemsSource="{Binding PersonList}"
                 SelectedItem="{Binding SelectedPerson}"
                 DisplayMemberPath="Name"
                 SelectedValuePath="City">

如果您不打算在派生的ListBox中拥有其他属性,则也可以根本不派生任何控件,而在声明它时将ControlTemplate分配给ListBox即可:

<Window.Resources>
    <ControlTemplate x:Key="MyListBoxTemplate">
        ...
    </ControlTemplate>
</Window.Resources>
...

<ListBox Template="{StaticResource MyListBoxTemplate}"
         ItemsSource="{Binding PersonList}"
         SelectedItem="{Binding SelectedPerson}"
         DisplayMemberPath="Name"
         SelectedValuePath="City">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM