【问题标题】:Simple User Control Databinding简单的用户控件数据绑定
【发布时间】:2013-04-02 05:22:43
【问题描述】:

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

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

    <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;
        }
    }
    
  2. 测试应用

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <uc:SimpleUserControl LineVisible="{Binding class1.Vis}"/>
    </Grid>
    
    public partial class MainPage : PhoneApplicationPage
    {
        public Class1 class1 { get; set; }
        public MainPage()
        {
            InitializeComponent();
            DataContext = this;
        }
        private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
        {
            class1 = new Class1() { Vis = false };
        }
    }
    
  3. 它绑定到的class1

    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"/>

我确定这很简单,但我没有看到它。 感谢您的帮助。

【问题讨论】:

    标签: xaml data-binding dependency-properties


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      1. 将 LineVisibiltyProperty 更改为 Visibility 而不是 bool。
      2. 使用转换器绑定到bool 并转换为Visibility

      我建议使用第二个选项,因为我认为这是更好的解决方案。

      This 可能会有所帮助。

      【讨论】:

      • 您好,感谢您的回复。依赖属性工作正常,只是testapp中的绑定似乎没有设置依赖属性。如果我将它明确设置为“True”或“False”,它会起作用,只有当我绑定它不起作用时。我认为这是因为我在 testapp 和 usercontrol 上设置 DataContext 的方式是错误的。
      • 我看到这篇文章解释了我做错了什么:[scottlogic.co.uk/blog/colin/2012/02/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多