WPF 自定义的DependencyProperty属性不被识别
定义了一个多选的Comobox,自定义依赖属性在调用时不被识别,不知道为什么。
调用部分提示ItemsSource 和SelectedItems 无法识别或访问成员
public partial class MulitComboBoxUserControl : UserControl, INotifyPropertyChanged
{
public MulitComboBoxUserControl()
{
InitializeComponent();
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<MultiComboBoxModel>), typeof(MulitComboBoxUserControl),new UIPropertyMetadata(null));
public ObservableCollection<MultiComboBoxModel> ItemsSource
{
get { return (ObservableCollection<MultiComboBoxModel>)GetValue(ItemsSourceProperty); }
set
{
SetValue(ItemsSourceProperty, value);
}
}
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(ObservableCollection<MultiComboBoxModel>), typeof(MulitComboBoxUserControl), new PropertyMetadata(null,null));
public ObservableCollection<MultiComboBoxModel> SelectedItems
{
get { return (ObservableCollection<MultiComboBoxModel>)GetValue(SelectedItemsProperty); }
set
{
SetValue(SelectedItemsProperty, value);
}
}
private string _selectedText;
public string SelectedText
{
get { return _selectedText; }
set
{
_selectedText = value;
this.NotifyPropertyChanged("SelectedText");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
MultiComboBoxModel model = sender as MultiComboBoxModel;
if (model != null)
{
IEnumerable<MultiComboBoxModel> items = ItemsSource.Where(b => b.IsSelected == true);
SelectedItems = new ObservableCollection<MultiComboBoxModel>(items);
StringBuilder builder = new StringBuilder();
foreach (var item in items)
{
builder.Append(item.Title + " ");
}
SelectedText = builder == null ? string.Empty : builder.ToString();
}
}
}
public class MultiComboBoxModel : INotifyPropertyChanged
{
private string _title;
private bool _isSelected;
#region Properties
public string Title
{
get
{
return _title;
}
set
{
_title = value;
NotifyPropertyChanged("Title");
}
}
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
private int _id;
public int Id
{
get { return _id; }
set
{
_id = value;
this.NotifyPropertyChanged("Id");
}
}
private object _tag;
public object Tag
{
get { return _tag; }
set
{
_tag = value;
this.NotifyPropertyChanged("Tag");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<Grid>
<ComboBox Text="{Binding SelectedText}" IsEditable="True" ItemsSource="{Binding ItemsSource}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}" Click="CheckBox_Click"/>
<TextBlock Text="{Binding Title}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
调用部分提示ItemsSource 和SelectedItems 无法识别或访问成员
<control:MulitComboBoxUserControl Grid.Row="0" ItemsSource="{Binding ItemsSource}" SelectedItems="{Binding SelectedItems}"></control:MulitComboBoxUserControl>
...全文
相关推荐
C#中WPF依赖属性的正确学习方法
主要介绍了C#中WPF依赖属性的正确学习方法 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
WPF依赖项属性完整示例与实战解析
为了提高代码可维护性与团队协作效率,建议遵循如下编码规范:规范项推荐做法字段命名注册位置全部放在static构造函数中元数据设置使用明确指定影响范围回调函数定义为,避免实例捕获包装器访问级别依据需求设为publicprotected或internal示例模板:✅ 提示:使用nameof()提升重构安全性,避免硬编码字符串。在样式、主题和控件模板中,可能会多次为同一属性设置元数据。
深入了解WPF的依赖属性及数据绑定
依赖属性是WPF框架中用于实现数据绑定和属性值继承的核心机制之一。与普通属性相比,依赖属性不仅支持通过数据绑定来动态更新UI元素,还能够响应属性值变化时触发相关的UI更新。依赖属性是通过类来实现的,它是一个公开的静态只读字段,WPF中的大多数可视化和非可视化元素都使用依赖属性来支持这些高级功能。// 示例代码:定义一个依赖属性要理解依赖属性的注册过程,首先需要了解WPF属性系统的几个核心组件:DependencyProperty 类:这是依赖属性系统的基础类,用于声明依赖属性。
C# WPF上位机-动画
1、自定义路由事件 2、动画基础 3、简单线性动画实例 4、动画属性
wpf自定义图片控件,可画框,图片浏览
wpf自定义图片控件,可画框,图片浏览,不喜勿喷!


