绑定UserControl的datacontext

时间:2011-07-28 12:54:11

标签: c# silverlight-4.0 mvvm

有点工作,感谢我现在使用的答案:

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); }
         }
    }

在我看来模特:

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

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

奇怪的问题来自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>

如果我只有Visibility设置它运行良好,我可以看到我使用调试通过转换器。

如果同时使用两者,则不会弹出弹出窗口。(弹出窗口显示,而Popup.Show = false)。但如果我关闭弹出窗口:

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

然后通过我的函数打开它,它可以工作但不经过转换器。

有人可以解释一下那里发生了什么吗?

[编辑] 我没有将我的usercontrol专门绑定到专用对象,而是使用main xaml的datacontext,然后它完美地工作。 [/编辑]

1 个答案:

答案 0 :(得分:0)

您需要使用依赖属性

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

一个不错的教程 http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties

基本上你需要设置静态方法...这将注册属性并让你在XAML中绑定它。然后,您重新命名属性以获取并设置要在代码中使用的dependancy属性。

它看起来比它更难;)

编辑:

哦......抱歉也许我误解了..你只是像绑定showPopup一样绑定到viewmodel中的值?没有注册属性更改事件?

相关问题