简体   繁体   English

UserControl中依赖项属性列表框的ItemsSource

[英]Dependency Property ListBox's ItemsSource in UserControl

I develop new desktop app in C# WPF MVVM and I have a problem with data-binding. 我在C#WPF MVVM中开发了新的桌面应用程序,但是数据绑定存在问题。 I create my own UserControl 我创建自己的UserControl

<UserControl x:Class="Project.View.UserControls.BusListDeviceControl"
             x:Name="BusList"
             ...
             >
    <Grid Grid.Column="0" Grid.Row="0" Margin="7, 5">
        <Grid.RowDefinitions>
            <RowDefinition Height="60"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <ListBox Grid.Row="1" ItemsSource="{Binding ElementName=BusList, Path=ItemsSource}" Background="{Binding ElementName=BusList, Path=BackgroundColor}" BorderBrush="{x:Null}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="25" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Image Grid.Column="0" Margin="5" Source="{Binding IconPath}"/>
                        <TextBlock Grid.Column="1" Text="{Binding Name}" FontFamily="Century Gothic" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="16"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

Code-behind 后台代码

public partial class BusListDeviceControl : UserControl, INotifyPropertyChanged
    {
        ObservableCollection<Device> devices;
        string color;

        public static readonly DependencyProperty DevicesProperty =
            DependencyProperty.Register("Devices", typeof(ObservableCollection<Device>), 
            typeof(BusListDeviceControl), new PropertyMetadata (new ObservableCollection<Device>()));
        public ObservableCollection<Device> Devices
        {
            get { return this.devices; }
            set
            {
                this.devices = value;
                RaisePropertyChanged("Devices");
            }
        }

        public static readonly DependencyProperty BackgroundColorProperty =
        DependencyProperty.Register("BackgroundColor", typeof(string), typeof(BusListDeviceControl));
        public string BackgroundColor
        {
            get { return color; }
            set
            {
                this.color = value;
                RaisePropertyChanged("BackgroundColor");
            }
        }

        public BusListDeviceControl()
        {
            InitializeComponent();
        }

        internal void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

And I want use this UserControl in my mainView. 我想在mainView中使用此UserControl。

<UserControl x:Class="Project.View.UserControls.IdentificationControl"
             ...>
    <UserControl.DataContext>
        <vm:IdentificationControlViewModel/>
    </UserControl.DataContext>
    <Grid Grid.Column="0" Margin="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="70" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <local:BusListDeviceControl Devices="{Binding Buses[0].Devices}" BackgroundColor="White" Grid.Column="0" Grid.Row="0" Margin="7, 5"></local:BusListDeviceControl>
        </Grid>
    </Grid>
</UserControl>

In IdentificationControlViewModel.cs I have public ObservableCollection Buses { get; 在IdentificationControlViewModel.cs中,我有公共的ObservableCollection总线。 set; 组; } and every Bus has public ObservableCollection Devices { get; },每辆公交车都有公共的ObservableCollection设备{ set; 组; }. }。 When I put all UserControl xaml code to mainView, binding work, but when I want use only to have clean code, binding for ItemsSource doesn't work but for BackgroundColor binding work properly. 当我将所有UserControl xaml代码放到mainView时,绑定工作正常,但是当我只想使用干净的代码时,ItemsSource的绑定不起作用,而BackgroundColor绑定则正常工作。

How to properly binding Buses[0].Devices to ItemsSource in my UserControl? 如何将Bus [0] .Devices正确绑定到UserControl中的ItemsSource?

The problem is that you are not using DependencyProperty the right way. 问题是您没有以正确的方式使用DependencyProperty see here for more information. 有关更多信息,请参见此处

Basically, when you use a DependencyProperty then you do not use a field in the class, and the Properties should look like this: 基本上,当您使用DependencyProperty那么你使用类中的一个字段,属性应该是这样的:

public string BackgroundColor
{
    get { return (string)GetValue(BackgroundColorProperty); }
    set
    {
       SetValue(BackgroundColorProperty,value);
    }
 }

GetValue and SetValue are inherited from DependencyObject . GetValueSetValueDependencyObject继承。

also, when you use DependencyProperty you do not need to implement INotifyPropertyChanged 另外,当您使用DependencyProperty时,不需要实现INotifyPropertyChanged

your class shuld look like this: 您的课程应如下所示:

public partial class BusListDeviceControl : UserControl
    {

        public static readonly DependencyProperty DevicesProperty =
            DependencyProperty.Register("Devices", typeof(ObservableCollection<Device>), 
            typeof(BusListDeviceControl), new PropertyMetadata (new ObservableCollection<Device>()));
        public ObservableCollection<Device> Devices
        {
            get { return (ObservableCollection<Device>)GetValue(DevicesProperty ); }
            set
            {
                SetValue(DevicesProperty ,value);
            }
        }

        public static readonly DependencyProperty BackgroundColorProperty =
        DependencyProperty.Register("BackgroundColor", typeof(string), typeof(BusListDeviceControl));
        public string BackgroundColor
        {
            get { return (string)GetValue(BackgroundColorProperty);  }
            set
            {
                SetValue(BackgroundColorProperty,value);
            }
        }

        public BusListDeviceControl()
        {
            InitializeComponent();
        }
    }

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

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