我有一个带有依赖项属性的usercontrol.
public sealed partial class PenMenu : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public bool ExpandCollapse
{
get
{
return false;
}
set
{
//code
}
}
public static readonly DependencyProperty ExpandCollapseProperty = DependencyProperty.Register("ExpandCollapse", typeof(bool), typeof(PenMenu), null);
//some more code
}
Run Code Online (Sandbox Code Playgroud)
我在XAML页面中赋值为:
<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened"
ExpandCollapse="{Binding PenMenuVisible}" />
Run Code Online (Sandbox Code Playgroud)
但它没有在usercontrol中击中ExpandCollapse属性的GET-SET部分.所以我添加bool到bool转换器只是为了检查带有绑定的值是什么:
<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened"
ExpandCollapse="{Binding PenMenuVisible, Converter={StaticResource booleanToBooleanConverter}}" />
Run Code Online (Sandbox Code Playgroud)
在转换器中使用断点,我看到传递的值是正确的.它没有分配给依赖属性的可能原因是什么?
如果我说:在XAML页面中:
<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened"
ExpandCollapse="true"/>
Run Code Online (Sandbox Code Playgroud)
然后它命中usercontrol中的ExpandCollapse属性的GET-SET部分.我被卡住了.这很奇怪.请帮忙.
c# dependency-properties winrt-xaml windows-store-apps windows-8.1
我有一个WPF应用程序,我在代码隐藏中使用依赖属性,我想通过XAML声明设置.
例如
<l:SelectControl StateType="A" Text="Hello"/>
Run Code Online (Sandbox Code Playgroud)
所以在这个例子中我有一个UserControl被调用的SelectControl,它有一个属性StateType,在它的setter中操作一些其他的属性.
为了帮助说明问题,我Text在示例中调用了另一个属性,继续阅读,我将进一步解释.
Codebehind摘录......
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(SelectControl));
public String Text
{
get { return (String)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty StateTypeProperty = DependencyProperty.Register("StateType", typeof(String), typeof(SelectControl));
public String StateType
{
get { return (String)GetValue(StateTypeProperty) }
set
{
switch (value)
{
case "A":
AnotherPropertyBoolean = true;
break;
case "B":
AnotherPropertyBoolean = false;
break;
default:
// this is only …Run Code Online (Sandbox Code Playgroud) 我知道有很多关于该错误的问题(1、2、3、4、5等),但我找不到可以解释此错误原因并适合我的情况的问题。如果我错过了,请告诉我!
首先,我ItemsSource使用自定义类(不是ObservableCollection或任何其他 .NET 内置可观察集合)绑定到我的 DataGrid 。在向您展示它的代码之前,让我解释一下我的想法(我的假设可能是错误的)。
在我看来,要可绑定,集合必须至少实现IEnumerable和INotifyCollectionChanged。IEnumerable 以便视图获取要显示的项目(感谢该GetEnumerator方法)和 INotifyCollectionChanged 以便视图知道集合上的更改。
所以我最终上了这门课:
public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IEnumerable<TValue>, INotifyCollectionChanged
{
#region fields
private IDictionary<TKey, TValue> _innerDictionary;
#endregion
#region properties
public int Count { get { return _innerDictionary.Count; } }
public ICollection<TKey> Keys { get { return _innerDictionary.Keys; } }
public ICollection<TValue> Values { get { return _innerDictionary.Values; } } …Run Code Online (Sandbox Code Playgroud)