为什么在这种情况下绑定可见性不适用于RibbonPages?

时间:2019-03-20 10:44:14

标签: c# wpf xaml binding user-controls

我必须在1个窗口中放置2个用户控件,并具有切换它们的能力。我是通过RibbonControl中的RibbonPages实现的,并通过在MainWindow.xaml中与MainViewModel中的属性进行绑定来设置可见性。

我没有任何错误。但是可见性正确,并且仅当我在MainWindow中直接设置<components:UserControl_1 Grid.Row="1" Visibility="Visible"/>而不进行绑定时,才会显示UserControl_1。否则,始终显示UserControl_2。

我可以在调试器中看到,更改RibbonPage时,将调用函数SelectedPageChanged(),并且将切换MainVM中的可见性属性。因此从理论上讲应该可行。

我希望我的移植物清楚。

这是我的代码:

Ribbon.xaml

<UserControl x:Class="Ribbon"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon"
            xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
            xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core"
            xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
            xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
            mc:Ignorable="d" 
            DataContext="{Binding Ribbon, Source={StaticResource Locator}}"
            d:DesignHeight="50" d:DesignWidth="1024"            
             >
<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

<dxr:RibbonControl x:Name="RibbonControl" Grid.Row="0"
                           SelectedPageChanged="RibbonControl_SelectedPageChanged">
<dxr:RibbonDefaultPageCategory>
                <dxr:RibbonPage Caption="User Control1" Name="UserControl1"/>
                <dxr:RibbonPage Caption="User Control2" Name="UserControl2"/>
</dxr:RibbonDefaultPageCategory>
</dxr:RibbonControl>
    </Grid>
</UserControl>

Ribbon.xaml.cs

public partial class Ribbon
    {
        public static RibbonViewModel RibbonVm;
        public Ribbon()
        {
            InitializeComponent();
            RibbonVm = DataContext as RibbonViewModel;            
        }

        private void RibbonControl_SelectedPageChanged(object sender, RibbonPropertyChangedEventArgs e)
        {
            if (Globals.MainVm == null) Globals.MainVm = SimpleIoc.Default.GetInstance<MainViewModel>();
                if (((RibbonPage)e.NewValue).Name == "UserControl2")
            {
                Globals.MainVm.UserControl_1_Visibility = Visibility.Collapsed;
                Globals.MainVm.UserControl_2_Visibility = Visibility.Visible;
            }
            else
            {
                Globals.MainVm.UserControl_1_Visibility = Visibility.Visible;
                Globals.MainVm.UserControl_2_Visibility = Visibility.Collapsed;
            }           
        }
        }

Globals.MainVmstatic class Globals

的MainViewModel的静态属性。

MainViewModel

 public class MainViewModel : DevExpress.Mvvm.ViewModelBase
    {
        private Visibility _userControl_1_Visibility = Visibility.Visible;
        private Visibility _userControl_2_Visibility = Visibility.Hidden;


        public Visibility UserControl_1_Visibility
        {
            get { return __userControl_1_Visibility; }
            set
            {
                __userControl_1_Visibility= value;
                RaisePropertyChanged();
            }
        }

        public Visibility UserControl_2_Visibility
        {
            get { return __userControl_2_Visibility; }
            set
            {
                __userControl_2__Visibility= value;
                RaisePropertyChanged();
            }
        }        
    }

我使用DevExpress.Mvvm.BindableBase.RaisePropertiesChanged

MainWindow.xaml

        <components:Ribbon Grid.Row="0" />
        <components:UserControl_1 Grid.Row="1" Visibility="{Binding UserControl_1_Visibility}"/>
        <components:UserControl_2 Grid.Row="1" Visibility="{Binding UserControl_2_Visibility}"/>

MainWindow.xaml.cs

 public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = SimpleIoc.Default.GetInstance<MainViewModel>();
       }
    }

0 个答案:

没有答案