C# 依赖属性列表<;字符串>;在用户控制中
我在我的点网程序集中的用户控件中有一个依赖属性(字符串列表),如下所示C# 依赖属性列表<;字符串>;在用户控制中,c#,wpf,dependency-properties,C#,Wpf,Dependency Properties,我在我的点网程序集中的用户控件中有一个依赖属性(字符串列表),如下所示 public partial class ItemSelectionUserControl : UserControl { public List<string> AvailableItems { get { return (List<string>)this.GetValue(AvailableItemsProperty); } set { this.S
public partial class ItemSelectionUserControl : UserControl
{
public List<string> AvailableItems
{
get { return (List<string>)this.GetValue(AvailableItemsProperty); }
set { this.SetValue(AvailableItemsProperty, value); }
}
public static readonly DependencyProperty AvailableItemsProperty = DependencyProperty.Register(
"AvailableItems", typeof(List<string>), typeof(ItemSelectionUserControl), new FrameworkPropertyMetadata{BindsTwoWayByDefault =true});
public ItemSelectionUserControl()
{
InitializeComponent();
}
}
<UserControl
xmlns:ctrl="clr-namespace:HH.Windows.UserControls;assembly=HH.Windows.UserControls"
/>
// .....
<Grid>
<ctrl:ItemSelectionUserControl Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="3" AvailableItems="{Binding Path=CheckList}"/>
</Grid>
公共部分类ItemSelectionUserControl:UserControl
{
公共列表可用项
{
获取{返回(列表)this.GetValue(AvailableItemsProperty);}
set{this.SetValue(AvailableItemsProperty,value);}
}
public static readonly dependencProperty AvailableItemsProperty=dependencProperty.Register(
“AvailableItems”、typeof(列表)、typeof(ItemSelectionUserControl)、new FrameworkPropertyMetadata{BindsTwoWayByDefault=true});
公共项SelectionUserControl()
{
初始化组件();
}
}
我试图在另一个程序集中的另一个usercontrol中使用此usercontrol,如下所示
public partial class ItemSelectionUserControl : UserControl
{
public List<string> AvailableItems
{
get { return (List<string>)this.GetValue(AvailableItemsProperty); }
set { this.SetValue(AvailableItemsProperty, value); }
}
public static readonly DependencyProperty AvailableItemsProperty = DependencyProperty.Register(
"AvailableItems", typeof(List<string>), typeof(ItemSelectionUserControl), new FrameworkPropertyMetadata{BindsTwoWayByDefault =true});
public ItemSelectionUserControl()
{
InitializeComponent();
}
}
<UserControl
xmlns:ctrl="clr-namespace:HH.Windows.UserControls;assembly=HH.Windows.UserControls"
/>
// .....
<Grid>
<ctrl:ItemSelectionUserControl Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="3" AvailableItems="{Binding Path=CheckList}"/>
</Grid>
// .....
我可以看到CheckList的get访问器正在被调用,但它没有设置依赖属性“AvailableItems”。“AvailableItems”集合中的断点永远不会被调用。我做错了什么?您没有指定绑定的模式。也许它只是默认为单向的?
尝试:
{Binding Path=CheckList,Mode=TwoWay}
据我所知,如果WPF公开了一个依赖属性,它可能不会直接调用属性的setter。相反,它可以直接设置dependencProperty
。有关详细信息,请参阅。特别是本节:
依赖项属性可能在多个位置“设置”
以下是XAML示例,其中同一属性(背景)具有三个可能影响值的不同“设置”操作
要测试您的示例中是否发生了这种情况(并获得一个可以对设置值进行操作的通知),您可以尝试在FrameworkPropertyMetadata
e、 g
公共部分类ItemSelectionUserControl:UserControl
{
公共列表可用项
{
获取{返回(列表)this.GetValue(AvailableItemsProperty);}
set{this.SetValue(AvailableItemsProperty,value);}
}
公共静态只读从属属性AvailableItemsProperty=
DependencyProperty.Register(“AvailableItems”,
typeof(列表),typeof(ItemSelectionUserControl),
新框架属性元数据(OnAvailableItemsChanged)
{
BindsTwoWayByDefault=true
});
公共项SelectionUserControl()
{
初始化组件();
}
AvailableItemsChanged上的公共静态无效(
DependencyObject发送方,
DependencyPropertyChangedEventArgs(附件e)
{
//在此处设置断点以查看是否正在设置新值
var newValue=e.newValue;
Debugger.Break();
}
}
可能类型不匹配?有关如何调试WPF绑定的更多信息,请参阅。@Andrew-这是正确的。非常感谢你。我不知道WPF会直接调用依赖属性。嗨,吉米,没问题。你的问题格式很好,回答起来很简单。另一个WPF抓到你了!:)