为动态内容绑定ContentControl内容

Xas*_*ser 28 c# wpf xaml binding contentcontrol

我目前正在尝试通过使用ListView(作为选项卡)和带有绑定内容属性的ContentControl来实现带有隐藏选项卡的tabcontrol的功能.

我读了一下这个主题,如果我做对了,它应该这样工作:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20.0*"/>
        <ColumnDefinition Width="80.0*"/>
    </Grid.ColumnDefinitions>
    <ListBox Grid.Column="0">
        <ListBoxItem Content="Appearance"/>
    </ListBox>

    <ContentControl Content="{Binding SettingsPage}" Grid.Column="1"/>
</Grid>
.
.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ContentControl x:Key="AppearancePage">
        <TextBlock Text="Test" />
    </ContentControl>
    <ContentControl x:Key="AdvancedPage">
        <TextBlock Text="Test2" />
    </ContentControl>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

并在代码背后:

public partial class MainWindow : MetroWindow
  {
    private ContentControl SettingsPage;
    private ResourceDictionary SettingsPagesDict = new ResourceDictionary();

    public MainWindow()
    {
        InitializeComponent();

        SettingsPagesDict.Source = new Uri("SettingsPages.xaml", UriKind.RelativeOrAbsolute);
        SettingsPage = SettingsPagesDict["AppearancePage"] as ContentControl;
Run Code Online (Sandbox Code Playgroud)

尽管它没有抛出任何错误,但它不会显示"Test"TextBlock.

我可能有错误的绑定概念,请给我一个正确方向的提示.

问候

fai*_*ing 75

好的我已经敲了一个简单的例子来向您展示如何使用带有数据绑定的MVVM(Model-View-ViewModel)方法动态更改ContentControl的内容.

我建议您创建一个新项目并加载这些文件以查看它是如何工作的.

我们首先需要实现INotifyPropertyChanged接口.这将允许您定义自己的类,其属性将在对属性进行更改时通知UI.我们创建一个提供此功能的抽象类.

ViewModelBase.cs

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我们现在需要拥有数据模型.为简单起见,我创建了2个模型 - HomePage和SettingsPage.两个模型只有一个属性,您可以根据需要添加更多属性.

HomePage.cs

public class HomePage
{
    public string PageTitle { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

SettingsPage.cs

public class SettingsPage
{
    public string PageTitle { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我创建相应的ViewModel来包装每个模型.请注意,viewmodels继承自我的ViewModelBase抽象类.

HomePageViewModel.cs

public class HomePageViewModel : ViewModelBase
{
    public HomePageViewModel(HomePage model)
    {
        this.Model = model;
    }

    public HomePage Model { get; private set; }

    public string PageTitle
    {
        get
        {
            return this.Model.PageTitle;
        }
        set
        {
            this.Model.PageTitle = value;
            this.OnPropertyChanged("PageTitle");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

SettingsPageViewModel.cs

public class SettingsPageViewModel : ViewModelBase
{
    public SettingsPageViewModel(SettingsPage model)
    {
        this.Model = model;
    }

    public SettingsPage Model { get; private set; }

    public string PageTitle
    {
        get
        {
            return this.Model.PageTitle;
        }
        set
        {
            this.Model.PageTitle = value;
            this.OnPropertyChanged("PageTitle");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我们需要为每个ViewModel提供视图.即HomePageView和SettingsPageView.我为此创建了2个UserControls.

HomePageView.xaml

<UserControl x:Class="WpfApplication3.HomePageView"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
        <TextBlock FontSize="20" Text="{Binding Path=PageTitle}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

SettingsPageView.xaml

<UserControl x:Class="WpfApplication3.SettingsPageView"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBlock FontSize="20" Text="{Binding Path=PageTitle}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

我们现在需要为MainWindow定义xaml.我已经包含了2个按钮来帮助在2个"页面"之间导航. MainWindow.xaml

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication3"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate DataType="{x:Type local:HomePageViewModel}">
        <local:HomePageView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:SettingsPageViewModel}">
        <local:SettingsPageView />
    </DataTemplate>
</Window.Resources>
<DockPanel>
    <StackPanel DockPanel.Dock="Left">
        <Button Content="Home Page" Command="{Binding Path=LoadHomePageCommand}" />
        <Button Content="Settings Page" Command="{Binding Path=LoadSettingsPageCommand}"/>
    </StackPanel>

    <ContentControl Content="{Binding Path=CurrentViewModel}"></ContentControl>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)

我们还需要一个用于MainWindow的ViewModel.但在此之前,我们需要创建另一个类,以便我们可以将我们的Buttons绑定到命令.

DelegateCommand.cs

public class DelegateCommand : ICommand
{
    /// <summary>
    /// Action to be performed when this command is executed
    /// </summary>
    private Action<object> executionAction;

    /// <summary>
    /// Predicate to determine if the command is valid for execution
    /// </summary>
    private Predicate<object> canExecutePredicate;

    /// <summary>
    /// Initializes a new instance of the DelegateCommand class.
    /// The command will always be valid for execution.
    /// </summary>
    /// <param name="execute">The delegate to call on execution</param>
    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Initializes a new instance of the DelegateCommand class.
    /// </summary>
    /// <param name="execute">The delegate to call on execution</param>
    /// <param name="canExecute">The predicate to determine if command is valid for execution</param>
    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        this.executionAction = execute;
        this.canExecutePredicate = canExecute;
    }

    /// <summary>
    /// Raised when CanExecute is changed
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    /// <summary>
    /// Executes the delegate backing this DelegateCommand
    /// </summary>
    /// <param name="parameter">parameter to pass to predicate</param>
    /// <returns>True if command is valid for execution</returns>
    public bool CanExecute(object parameter)
    {
        return this.canExecutePredicate == null ? true : this.canExecutePredicate(parameter);
    }

    /// <summary>
    /// Executes the delegate backing this DelegateCommand
    /// </summary>
    /// <param name="parameter">parameter to pass to delegate</param>
    /// <exception cref="InvalidOperationException">Thrown if CanExecute returns false</exception>
    public void Execute(object parameter)
    {
        if (!this.CanExecute(parameter))
        {
            throw new InvalidOperationException("The command is not valid for execution, check the CanExecute method before attempting to execute.");
        }
        this.executionAction(parameter);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我们可以定义MainWindowViewModel.CurrentViewModel是绑定到MainWindow上的ContentControl的属性.当我们通过单击按钮更改此属性时,屏幕将在MainWindow上更改.由于我在Window.Resources部分中定义的DataTemplates,MainWindow知道要加载哪个屏幕(usercontrol).

MainWindowViewModel.cs

public class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
        this.LoadHomePage();

        // Hook up Commands to associated methods
        this.LoadHomePageCommand = new DelegateCommand(o => this.LoadHomePage());
        this.LoadSettingsPageCommand = new DelegateCommand(o => this.LoadSettingsPage());
    }

    public ICommand LoadHomePageCommand { get; private set; }
    public ICommand LoadSettingsPageCommand { get; private set; }

    // ViewModel that is currently bound to the ContentControl
    private ViewModelBase _currentViewModel;

    public ViewModelBase CurrentViewModel
    {
        get { return _currentViewModel; }
        set
        {
            _currentViewModel = value; 
            this.OnPropertyChanged("CurrentViewModel");
        }
    }

    private void LoadHomePage()
    {
        CurrentViewModel = new HomePageViewModel(
            new HomePage() { PageTitle = "This is the Home Page."});
    }

    private void LoadSettingsPage()
    {
        CurrentViewModel = new SettingsPageViewModel(
            new SettingsPage(){PageTitle = "This is the Settings Page."});
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,我们需要覆盖应用程序启动,以便我们可以将MainWindowViewModel类加载到MainWindow的DataContext属性中.

App.xaml.cs

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        var window = new MainWindow() { DataContext = new MainWindowViewModel() };
        window.Show();
    }
}
Run Code Online (Sandbox Code Playgroud)

删除StartupUri="MainWindow.xaml"App.xaml Application标记中的代码也是一个好主意,这样我们就不会在启动时获得2个MainWindows.

请注意,DelegateCommand和ViewModelBase类只能复制到新项目中并使用.这只是一个非常简单的例子.你可以从这里这里得到一个更好的主意

编辑 在您的评论中,您想知道是否可以不必为每个视图和相关的样板代码创建一个类.据我所知,答案是否定的.是的,您可以拥有一个巨大的类,但您仍然需要为每个Property setter调用OnPropertyChanged.这也有很多弊端.首先,由此产生的类很难维护.会有很多代码和依赖项.其次,使用DataTemplates来"交换"视图会很困难.仍然可以使用ax:键入您的DataTemplates并在您的usercontrol中对模板绑定进行硬编码.从本质上讲,你并没有真正使你的代码变短,但是你会让自己变得更难.

我猜你的主要抱怨是你必须在你的viewmodel中编写这么多代码来包装你的模型属性.看看T4模板.一些开发人员使用它来自动生成他们的样板代码(即ViewModel类).我个人不使用这个,我使用自定义代码片段来快速生成viewmodel属性.

另一种选择是使用MVVM框架,例如Prism或MVVMLight.我自己没有使用过,但我听说其中一些内置功能可以轻松实现样板代码.

另一点需要注意的是:如果要将设置存储在数据库中,则可以使用ORM框架(如Entity Framework)从数据库生成模型,这意味着您剩下的就是创建视图模型和视图.

  • 别担心.有关过多代码的问题,请参阅上面的编辑. (2认同)
  • 我不会真的称之为gui行为,这是更多的业务逻辑.上面显示的方法是MVVM的"ViewModel First"方法,这基本上意味着我们通过数据控制应用程序.我们根据需要创建和处理视图模型.实际的"gui行为"发生在视图中,该视图由数据模板定义.使用这种方法,您可以完全为同一个视图模型创建一个新的用户控件,并使用相同的pagetitle属性作为文本框而不是文本块,而无需更改您的viewmodel代码. (2认同)
  • `如果你愿意的话,你可以轻松地在资源中连接一个不同的视图模型.这就是困扰我的事情,如果我想拥有100个视图,我将不得不将100个视图模型连接到`资源中的那100个视图`.我想要的是`<DataTemplate DataType ="{x:Type vm:{Binding currentViewModelType}}"> <v:{Binding currentViewType} </ DataTemplate>`.你觉得我在这里吗? (2认同)