Xaml 简单用户控制数据绑定

Xaml 简单用户控制数据绑定,xaml,data-binding,dependency-properties,Xaml,Data Binding,Dependency Properties,我一直在尝试用dependencyproperty创建一个简单的用户控件并对其进行绑定,但它似乎不起作用,不知道为什么。我将直接深入代码,请忽略控件没有意义这一事实,它只是为了说明目的(如果有必要的话,用WP8编写) 我的简单用户控件,它基本上是一行,带有一个属性来关闭或打开它 <Grid x:Name="LayoutRoot" Background="Transparent"> <Line Height="105" Width="105" X2="100" Y2="100"

我一直在尝试用dependencyproperty创建一个简单的用户控件并对其进行绑定,但它似乎不起作用,不知道为什么。我将直接深入代码,请忽略控件没有意义这一事实,它只是为了说明目的(如果有必要的话,用WP8编写)

  • 我的简单用户控件,它基本上是一行,带有一个属性来关闭或打开它

    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Line Height="105" Width="105" X2="100" Y2="100" Visibility="{Binding LineVisible}" Stroke="#FFFC1515" StrokeThickness="5"/>
    </Grid>
    
    public partial class SimpleUserControl : UserControl
    {
        public SimpleUserControl()
        {
            InitializeComponent();
            DataContext = this;
        }
    
        public static readonly DependencyProperty LineVisibleProperty = DependencyProperty.Register("LineVisible", typeof(bool), typeof(SimpleUserControl), new PropertyMetadata(new PropertyChangedCallback(OnLineVisibleChanged)));
        public bool LineVisible
        {
            get { return (bool)GetValue(LineVisibleProperty); }
            set { SetValue(LineVisibleProperty, value); }
        }
        private static void OnLineVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool newvalue = (bool)e.NewValue;
            Visibility vis = newvalue ? Visibility.Visible : Visibility.Collapsed;
            (d as SimpleUserControl).Visibility = vis;
        }
    }
    
  • 但是,如果像下面这样显式地设置它,它似乎不起作用

    <uc:SimpleUserControl LineVisible="False"/>
    
    
    
    我相信这很简单,但我没看到。
    感谢您的帮助。

    WPF控件的
    可见性
    属性不使用
    bool
    值,它需要
    可见性
    。因此,您有两种选择:

  • 将LineVisibilityProperty更改为
    可见性
    ,而不是布尔
  • 使用转换器绑定到
    bool
    并转换为
    可见性
  • 我建议使用第二种选择,因为我认为这是更好的解决方案


    可能会有帮助。

    问题是我在UserControl中设置了DataContext=this,当绑定到testapp中的Vis时,它会覆盖并搜索UserControl中的Vis(当然,那里不存在)。我确实在调试输出窗口中看到绑定错误,这证实了这一点。解决方案是将UserControl的LayoutRoot设置为此,正如我之前发布的链接中所述。

    Hi,感谢您的回复。dependencyproperty工作得很好,只是testapp中的绑定似乎没有设置dependencyproperty。如果我显式地将其设置为“True”或“False”,则只有当我绑定它不起作用时,它才起作用。我认为这是因为我在testapp和usercontrol上设置DataContext的方式都是错误的。我遇到了这篇文章,它解释了我做错了什么:[
    public class Class1 : INotifyPropertyChanged
    {
        private bool _vis;
        public bool Vis
        {
            get { return _vis; }
            set
            {
                _vis = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Vis"));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
    
    <uc:SimpleUserControl LineVisible="False"/>