WPF 自定义的DependencyProperty属性不被识别

saybookcat 2017-10-30 01:48:49
定义了一个多选的Comobox,自定义依赖属性在调用时不被识别,不知道为什么。

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>

...全文
371 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
saybookcat 2017-10-30
  • 打赏
  • 举报
回复
引用 1 楼 qq_28194303 的回复:
我也是遇到过,之前试过取消掉 new UIPropertyMetadata(null) 默认值的参数重新编译下就正常了。
把版本从.net framework 4.0 t提升到4.5就可以编译过了。。。。不懂为什么
晨易夕 2017-10-30
  • 打赏
  • 举报
回复
我也是遇到过,之前试过取消掉 new UIPropertyMetadata(null) 默认值的参数重新编译下就正常了。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧