使用用户定义的XAML添加控件以在运行时形成

时间:2014-09-02 15:54:46

标签: c# wpf xaml

我希望允许我的用户通过导入XAML在我的程序中定义他们的控件。

作为一个简单的例子,假设用户想要添加一个网格,他们可以导入下面的XAML。如何将此添加到论坛中。

<Grid 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="1,2,1,2">
    <Grid.RowDefinitions>
        <RowDefinition Height="60" />
        <RowDefinition Height="60" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="45" />
        <ColumnDefinition Width="45" />
        <ColumnDefinition Width="45" />
        <ColumnDefinition Width="45" />
        <ColumnDefinition Width="45" />
    </Grid.ColumnDefinitions>

    <Button Grid.Row="0" Grid.Column="0" Content="1" FontSize="14" Margin="1,2,1,2" FontWeight="Bold" />    
</Grid>

2 个答案:

答案 0 :(得分:1)

这是对ethicallogics答案的类似答案,但我不喜欢他在他的ViewModel中引用FrameworkElement。这将您的ViewModel耦合到WPF。相反,我将用户的内容加载到ViewModel中的字符串属性中。

<强>视图模型

public string DynamicXaml
{
    get { return _dynamicXaml; }
    set
    {
       if (_dynamicXaml != value)
       {
           _dynamicXaml = value;
           RaisePropertyChanged(() => DynamicXaml);
       }
    }
}

然后创建一个转换器以将字符串转换为FrameworkElement。

<强>转换器

[ValueConversion(typeof(string), typeof(FrameworkElement))]
public class XamlStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        FrameworkElement result = null;
        string xaml = value as string;
        if (!string.IsNullOrEmpty(xaml))
        {
            try
            {
                result = XamlReader.Parse(xaml) as FrameworkElement;
            }
            catch (Exception ex)
            {
                //add logging logic here.
            }
        }
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

最后,您可以使用ContentControl或ContentPresenter来显示自定义xaml。

<强> XAML

<ContentControl x:Name="DynamicControl"
                Content="{Binding Path=DynamicXaml, Converter={StaticResource XamlConverter}}"/>

答案 1 :(得分:-1)

First让我们创建一个AttachedProperty,以便加载的xaml可以添加到我们想要的任何Panel或ContentControl中。

public class MyFrameworkObject : DependencyObject
{
    public static readonly DependencyProperty RuntimeFrameWorkElementProperty = DependencyProperty.RegisterAttached("RuntimeFrameWorkElement", typeof(FrameworkElement), typeof(MyFrameworkObject),new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));


    static void  OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        SetRuntimeFrameWorkElement(d as UIElement, e.NewValue as FrameworkElement);
    }
    public static void SetRuntimeFrameWorkElement(UIElement element, FrameworkElement value)
    {
        if (element!=null && value != null && value.Parent == null) //The loaded Control can be added to only one Control because its Parent will be set
        {
            var panel = element as Panel;
            if (panel != null)
            {
                panel.Children.Add(value);
                return;
            }
            var contentControl = element as ContentControl;
            if (contentControl != null)
                contentControl.Content = value;

            //TODO:ItemsControl
        }
    }
}
  

ViewModel:在ViewModel中,允许创建并加载可绑定到上面附加属性的属性。

    public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        LoadXaml();
    }

    FrameworkElement frameWorkElement;

    public FrameworkElement RuntimeFrameWorkElement
    {
        get { return frameWorkElement; }
        set { frameWorkElement = value; OnPropertyChanged("RuntimeFrameWorkElement"); }
    }

    public void LoadXaml()
    {
        FileInfo f = new FileInfo(@"F:\myxaml.txt"); //Load xaml from some external file
        if (f.Exists)
            using (var stream = f.OpenRead())
            {
                this.RuntimeFrameWorkElement = XamlReader.Load(stream) as FrameworkElement;
            }
    }

    void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
  

xaml.cs

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
  

xaml允许使用attachedProperty

<Window x:Class="StackoverflowQues.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:StackoverflowQues"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Button  Content="Ok"/>
    <Grid  local:MyFrameworkObject.RuntimeFrameWorkElement="{Binding RuntimeFrameWorkElement}"></Grid>
</StackPanel>

  

Simillarly你可以将这个附加属性绑定到任何面板,如Stack,Dock,Wrap,Grid

<StackPanel local:MyFrameworkObject.RuntimeFrameWorkElement="{Binding RuntimeFrameWorkElement}">
</StackPanel>
  

或者也可以绑定到ContentControl或ItemsControl(还有)

     

输出我使用了与你相同的xaml

enter image description here

注意:如果您将此附加属性指定给两个或多个面板或控件,则它将仅添加到第一个面板或控件。因为那时将加载已加载的xaml控件的Parent,并且无法添加另一个控件。