WPF绑定不到父控件属性
2018-03-07 17:03
519 查看
原因:
如果A控件并不是属于visual tree的部分,那么他不能连接到他父控件的datacontext,也就不能绑定到父控件的属性
解决方案:
使用一个Freezable做代理
然后在XML文件中添加静态资源,将代理的Data绑定为父控件
最后在A控件中,通过静态资源的Data来绑定父控件的属性
如果A控件并不是属于visual tree的部分,那么他不能连接到他父控件的datacontext,也就不能绑定到父控件的属性
解决方案:
使用一个Freezable做代理
public class BindingProxy : Freezable { #region Overrides of Freezable protected override Freezable CreateInstanceCore() { return new BindingProxy(); } #endregion public object Data { get { return (object)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc... public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); }
然后在XML文件中添加静态资源,将代理的Data绑定为父控件
<UserControl.Resources> <ResourceDictionary> <pubEntity:BindingProxy x:Key="Proxy" Data="{Binding}" /> </ResourceDictionary> </UserControl.Resources>
最后在A控件中,通过静态资源的Data来绑定父控件的属性
Command="{Binding Data.DownLoadCommand,Source={StaticResource Proxy}}"
相关文章推荐
- WPF绑定不到父控件属性
- wpf 控件的依赖属性和数据绑定
- WPF绑定各种数据源之元素控件属性
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
- WPF 如何在后台进行控件属性的绑定MVVM
- 【WPF】对Frame控件的Content属性做绑定时出现的一个小问题
- 【转】WPF中PasswordBox控件的Password属性的数据绑定
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
- WPF 依赖属性,用户控件依赖属性(DependencyProperty 依赖属性、GetValue() SetValue() CLR属性包装器、SetBinding 数据绑定)
- WPF绑定各种数据源之元素控件属性
- WPF中PasswordBox控件无法绑定Password属性解决办法
- WPF中一个控件绑定另一个控件的属性
- WPF自定义控件,将该控件中包含的控件的某个属性绑定到自定义的属性对外暴露
- 【WPF】WPF通过RelativeSource绑定父控件的属性
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
- WPF学习笔记 - 与.Net对象属性绑定
- ASP.Net控件模板中根据绑定对象的属性控制元素隐现技巧
- 如何得到WPF中控件绑定的EventTrigger
- WPF中DataGrid控件内Button的Command和CommandParameter的绑定
- wp8和wpf里监听控件自带属性的变化