0

いくつかのプロパティを持つカスタム UserControl (MyControl) を取得しました (これは問題なく動作します)。UserControl を使用するページにコンテンツを「貼り付け」、UserControl に直接表示できるようにする新しいプロパティが必要です。通り。私が試してみました; ContentPresenter、ContentControl、StackPanel、運が悪い...

MyControl.xaml

<ContentControl Grid.Column="0" Content="{Binding MyContent, ElementName=root}"></ContentControl>

MyControl.xaml.cs

public object MyContent
{
  get { return (object)GetValue(MyContentProperty); }
  set { SetValue(MyContentProperty, value); }
}

public static readonly DependencyProperty MyContentProperty =
  DependencyProperty.Register("MyContent", typeof(object), typeof(MyControl), new PropertyMetadata(null));

SomePage.xml

<mycontrols:MyControl x:Name="FavoritesButton">
  <mycontrols:MyControl.MyContent>
    <Path Data="M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07z" Stretch="Uniform" Fill="#FFFFFFFF" Width="50" Height="50" Margin="30"></Path>
  </mycontrols:MyControl.MyContent>
</mycontrols:MyControl>
4

2 に答える 2

1

私は本当にうまくいく次のものを持っています。(MVVMの原則に多少反していますが...メインウィンドウの単一フレーム領域でユーザーコントロールを動的に処理するのが好きです)

私の MainWindow.xaml:

<!-- Main Frame -->
<Grid Grid.Column="1" Margin="10" Name="MainWindowFrameContent">
   <ItemsControl ItemsSource="{Binding Path=MainWindowFrameContent}" >

      <!-- This controls the height automatically of the user control -->
      <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
            <UniformGrid Columns="1" IsItemsHost="True"/>
         </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
   </ItemsControl>
</Grid>

私の MainViewModel.cs:

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using myProject.View;
using myProject.Models;

namespace myProject.ViewModel
{
    public class MainViewModel : ObservableObject
    {
        public MainViewModel() { }

        // This handles adding framework (UI) elements to the main window frame
        ObservableCollection<FrameworkElement> _MainWindowFrameContent = new ObservableCollection<FrameworkElement>();
        public ObservableCollection<FrameworkElement> MainWindowFrameContent
        {
            get { return _MainWindowFrameContent; }
            set { _MainWindowFrameContent = value; RaisePropertyChangedEvent("MainWindowFrameContent"); }
        }
    }
}

MainViewModel.cs は「パブリック クラス MainViewModel : ObservableObject」です。これにより、「RaisePropertyChangedEvent」を実装して、「MainWindowFrameContent」の値を変更したときにバインディングが正常に更新されるようにすることができます。

私の ObservableObject.cs:

using System.ComponentModel;

namespace myProject.ViewModel
{
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChangedEvent(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

次に、MainWindowFrameContent に項目を追加する場合は、MainViewModel.cs 内で次の操作を行うだけです。

void _AddNewUserControl()
{
    myUserControl hControl = new myUserControl();
    MainWindowFrameContent.Clear(); // Clear the frame before displaying new content
    MainWindowFrameContent.Add(hControl);
}

次に、必要な数のユーザー コントロールを作成できます。表示する各コマンドは、VM で独自の void _AddNewUserControl タイプ メソッドを持つことができ、メイン ウィンドウに表示されます。繰り返しますが、これは MVVM フレームワークとは少し異なりますが、これはコード ベースからかなりきれいに保たれています。

于 2016-06-11T18:40:58.327 に答える