简体   繁体   English

绑定用户控件的数据上下文

[英]Binding the datacontext of a UserControl

Having work a bit and thanks to the answer I am now using : 有一点工作,感谢我现在正在使用的答案:

public class PopupProgramazione : DependencyObject
    {
        public static readonly DependencyProperty ShowProperty = DependencyProperty.Register("FirstNo", typeof(bool), typeof(PopupProgramazione), null);

        public bool Show
         {
            get { return (bool)GetValue(ShowProperty); }
            set { SetValue(ShowProperty, value); }
         }
    }

in my view Model: 在我看来,型号:

   public PopupProgramazione Popup
    {
        get { return _Popup; }
        set
        {
            _Popup = value;
            RaisePropertyChanged("Popup");
        }
    }

    public void Programmazione(InterventoSchedeConsuntivi intervento)
    {
        Popup.Show = true;
        InterventoPopupProgramazione = intervento;
    }

the strange problem comes with the xaml : xaml带来了一个奇怪的问题:

<local:PopupProgrammazione 
             x:Name="popupProg"
            Height="300" 
            Width="400"
            Canvas.ZIndex="2"
            Canvas.Left="150"
            Canvas.Top="150" Grid.RowSpan="4" Grid.Column="2" Margin="7,4,12,296"
            Visibility="{Binding Path=Popup.Show, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}}"
            DataContext="{Binding Path=InterventoPopupProgramazione}"
            >
        </local:PopupProgrammazione>

If I have only Visibility set it works well and I can see that I am getting through the converter using the debug. 如果仅设置了“可见性”,则它可以很好地运行,并且可以看到我正在使用调试程序通过转换器。

If have both, then the initialisation of the popup is not made.( the popup is shown whereas Popup.Show=false). 如果两者都存在,则不对弹出窗口进行初始化。(显示弹出窗口,而Popup.Show = false)。 But If I close the popup : 但是,如果我关闭弹出窗口:

private void Close_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

and then pass by my function to open it, it works but without passing by the converter. 然后通过我的函数将其打开,它可以工作,但无需通过转换器。

Could someone explain me what is going on there? 有人可以解释一下那里发生了什么吗?

[EDIT] Instead of binding specifically my usercontrol to a dedicated object, I use the datacontext of main xaml and then it works perfectly. [EDIT]我没有将我的用户控件专门绑定到专用对象,而是使用main xaml的数据上下文,然后它可以完美地工作。 [/EDIT] [/编辑]

You need to use a dependancy property 您需要使用依赖项属性

http://msdn.microsoft.com/en-us/library/ms752914.aspx http://msdn.microsoft.com/en-us/library/ms752914.aspx

A decent tutorial http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties 体面的教程http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties

basically you need to set up the static method ... this will register the property and let you bind to it in XAML. 基本上,您需要设置静态方法...这将注册该属性,并允许您在XAML中将其绑定。 You then repoint your property to get and set the dependancy property for use in your code. 然后,您重新指向属性,以获取并设置要在代码中使用的依赖属性。

It looks a lot harder than it is ;) 它看起来比实际要难得多;)

Edit: 编辑:

oh ... sorry maybe i misunderstood .. cant you just bind to the value that is in the viewmodel just like you did for showPopup? 哦...对不起,也许我误会了..就像您为showPopup所做的那样,您不能将其绑定到viewmodel中的值吗? Failing that register with the property changed event? 未能通过属性更改事件注册?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM