我得到了一个自定义UserControl (MyControl)与几个属性(这工作得很好)。我想要一个新的属性,让页面使用UserControl"粘贴"一些内容直接显示在UserControl -例如路径。我试过了;ContentPresenter, ContentControl, StackPanel, with no luck…
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>
我有以下工作真的很好。(虽然这有点违背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框架有点相反,但它从代码库中保持了相当干净。
我让它工作…解决方案如下:
MyControl.xaml
<ContentControl Content="{Binding Shape, ElementName=root}" />
MyControl.xaml.cs
public Shape Shape
{
get { return (Shape)GetValue(ShapeProperty); }
set { SetValue(ShapeProperty, value); }
}
public static readonly DependencyProperty ShapeProperty =
DependencyProperty.Register("Shape", typeof(Shape), typeof(MyControl), new PropertyMetadata(null));
SomePage.xml
<mycontrols:MyControl>
<mycontrols:MyControl.Shape>
<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" Style="{StaticResource PathStyle}" />
</mycontrols:MyControl.Shape>
</mycontrols:MyControl>