将自动控件中的列表框绑定.Cannot绑定它

时间:2016-07-25 13:30:23

标签: wpf wpf-controls

将自定义控件中的列表框绑定

我正在构建一个自定义控件,里面会有一个列表框。我在这里发布了简化版

我似乎无法显示绑定的viewModel的属性。“Surname”。

你能看出我做错了吗?

Generic.Xaml(在CustomControl内)

        <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WpfCustomControlLibrary1.SimpleList">
        <Style TargetType="{x:Type local:SimpleListControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:SimpleListControl}">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">

                            <Grid ShowGridLines="False" 
                                  DataContext="{Binding RelativeSource={RelativeSource 
                                                                        FindAncestor, 
                                                                        AncestorType={x:Type local:SimpleListControl}}}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="25"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <StackPanel Orientation="Vertical" Grid.Column="0" Grid.Row="0">
                                    <ListBox Name="lstLeft" 
                                              MinHeight="200"
                                              ItemsSource="{Binding LeftSideList, RelativeSource={RelativeSource TemplatedParent}}"
                                              DisplayMemberPath="{Binding DisplayMemberPath, RelativeSource={RelativeSource TemplatedParent}}"
                                              SelectionMode="Extended"
                                              IsSynchronizedWithCurrentItem="True"
                                              SelectedIndex="0" />
                                </StackPanel>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>


        public class SimpleListControl : Control
        {
            static SimpleListControl()
            {
                DefaultStyleKeyProperty
                    .OverrideMetadata(typeof(SimpleListControl), 
                    new FrameworkPropertyMetadata(typeof(SimpleListControl)));

                LeftSideListProperty = DependencyProperty.Register("LeftSideList", 
                    typeof(IEnumerable), 
                    typeof(SimpleListControl), 
                    new PropertyMetadata(OnLeftItemsSourceChanged));

                DisplayMemberPathProperty = DependencyProperty.Register("DisplayMemberPath", 
                    typeof(string), 
                    typeof(SimpleListControl), 
                    new UIPropertyMetadata(string.Empty));
            }


            public readonly static DependencyProperty LeftSideListProperty;
            public ObservableCollection<object> LeftSideList
            {
                get{return new ObservableCollection<object>{GetValue(LeftSideListProperty)};}
                set { SetValue(LeftSideListProperty, value); }
            }

            public static readonly DependencyProperty DisplayMemberPathProperty;
            public string DisplayMemberPath
            {
                get { return (string)GetValue(DisplayMemberPathProperty); }
                set { SetValue(DisplayMemberPathProperty, value); }
            }

            private static void OnLeftItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                //TODO.
            }
        }

CustomerView

    <Window x:Class="WpfApp1.Views.CustomersView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:simpleList="clr-namespace:WpfCustomControlLibrary1.SimpleList;assembly=WpfCustomControlLibrary1"
        xmlns:viewModels="clr-namespace:WpfApp1.ViewModels"
        Title="CustomersViewq" Height="300" Width="300">

    <Window.Resources>
        <viewModels:CustomersViewModel x:Key="ViewModel" />
    </Window.Resources>
    <Grid>
        <simpleList:SimpleListControl Name="SimpleListControl1" 
                                LeftSideList="{Binding Source={StaticResource ViewModel},Path=AvailableCustomers}"
                               DisplayMemberPath="Surname" Margin="0,24,0,12" />
    </Grid>
</Window>

CustomersViewModel

public class CustomersViewModel:ViewModelBase
    {
    public CustomersViewModel()
    {
        availableCustomers = new ObservableCollection<CustomerViewModel>();
        availableCustomers.Add(new CustomerViewModel { FirstName = "Jo1", Surname = "Bloggs1" });
        availableCustomers.Add(new CustomerViewModel { FirstName = "Jo2", Surname = "Bloggs2" });
        availableCustomers.Add(new CustomerViewModel { FirstName = "Jo3", Surname = "Bloggs3" });
        availableCustomers.Add(new CustomerViewModel { FirstName = "Jo4", Surname = "Bloggs4" });
    }

    private ObservableCollection<CustomerViewModel> availableCustomers;

    public ObservableCollection<CustomerViewModel> AvailableCustomers
    {
        get { return availableCustomers; }
        set
        {
            if (availableCustomers == value) return;
            availableCustomers = value;
            OnPropertyChanged("AvailableCustomers");
        }
    }
}

CustomerViewModel

 public class CustomerViewModel : ViewModelBase
{
    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set
        {
            if (firstName == null) return;
            firstName = value;
            OnPropertyChanged("FirstName");
        }
    }

    private string surname;

    public string Surname
    {
        get { return surname; }
        set
        {
            if (surname == null) return;
            surname = value;
            OnPropertyChanged("Surname");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

无论你想要实现什么,似乎你都过于复杂化了。 但是,如果您需要显示Surname,则需要在DisplayMemberPath中将其设置为DisplayMemberPath="Surname"。 以下是自定义控件的教程:How to Create Custom Control

以下是创建可重用用户控件的链接: Creating Re-useable User Controls

相关问题