替换网格中的用户控件

时间:2013-09-09 03:17:20

标签: wpf xaml mvvm user-controls

我的xaml看起来像这样:

<UserControl>
    <Grid>
        ...
    <Grid>
    ...
    <Grid Grid.Row="3">
        <local:UserControl1 Visibility = "{Binding UserControl1Visibility}" />
        <local:UserControl2 Visibility = "{Binding UserControl2Visibility}" />
    </Grid>
</UserControl>

Control1Visibility和Control2Visibility是我的ViewModel中的属性,但它不起作用。此网格无法动态显示两个用户控件。有人有想法吗?

修改

VM:

private Visibility userControl1Visibility = Visibility.Visible;

        public Visibility UserControl1Visibility
        {
            get { return userControl1Visibility; }
            set
            {
                if (userControl1Visibility != value)
                {
                    userControl1Visibility = value;
                    RaisePropertyChanged("UserControl1Visibility");
                }
            }
        }


        private Visibility userControl2Visibility = Visibility.Collapsed;

        public Visibility UserControl2Visibility
        {
            get { return userControl2Visibility ; }
            set
            {
                if (userControl2Visibility != value)
                {
                    userControl2Visibility = value;
                    RaisePropertyChanged("UserControl2Visibility");
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

您的名称有误 - 在xaml中您将其称为Control1Visibility和Control2Visibility,但在ViewModel中,您将其称为UserControl1Visibility和UserControl2Visibility。更改其中一个名称,并确保使用相应的字符串引发PropertyChanged事件。