在运行时绑定XAML wpf

时间:2016-02-16 04:43:50

标签: wpf binding wpf-controls

我正在尝试将一些数据存储在XAML中并在运行时加载它。用于存储我的xaml看起来像这样。

<osp:OSPData x:Class="OptisetStore.Model.OSPData"
         xmlns:osp="clr-namespace:OptisetStore.Model;assembly=OptisetStore"
          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:local="clr-namespace:OptisetStore.Model"
          mc:Ignorable="d" 
          osp:OSPData.Name="OspCollection">

<osp:OSPData.Features>
    <osp:Feature LocalName="feature1" x:Name="F1" IsEnabled="True" />
    <osp:Feature LocalName="{Binding ElementName=F1, Path=LocalName, Mode=TwoWay}" IsEnabled="{Binding ElementName=F1, Path=IsEnabled, Mode=TwoWay}" />
</osp:OSPData.Features>

OSPData类

public partial class OSPData : DependencyObject
{
    public string Name { get; set; }

    public OSPData()
    {
        Features = new ObservableCollection<Feature>();
    }

    public ObservableCollection<Feature> Features
    {
        get { return (ObservableCollection<Feature>)GetValue(FeaturesProperty); }
        set { SetValue(FeaturesProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Features.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FeaturesProperty =
        DependencyProperty.Register("Features", typeof(ObservableCollection<Feature>), typeof(OSPData), new PropertyMetadata(new ObservableCollection<Feature>()));


}

要素类:

public  class Feature : DependencyObject
{

    public bool IsEnabled
    {
        get { return (bool)GetValue(IsEnabledProperty); }
        set { SetValue(IsEnabledProperty, value); }
    }

    // Using a DependencyProperty as the backing store for IsEnabled.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.Register("IsEnabled", typeof(bool), typeof(Feature), new PropertyMetadata(false));

    public string LocalName
    {
        get { return (string)GetValue(LocalNameProperty); }
        set { SetValue(LocalNameProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LocalName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LocalNameProperty =
        DependencyProperty.Register("LocalName", typeof(string), typeof(Feature), new PropertyMetadata(""));

}

所以在我的ospdata准备就绪后,我存储并在运行时加载类来填充我的UI。但元素名称绑定不起作用。

 StringReader stringReader = new StringReader(File.ReadAllText("Model/OSPData.xaml"));
        XmlReader xmlReader = XmlReader.Create(stringReader);
        var data = (OSPData)XamlReader.Load(xmlReader);


        SimpleIoc.Default.GetInstance<TestingViewModel>().Data = data;

我的ui看起来像这样:

<UniformGrid>
    <ListBox ItemsSource="{Binding Data.Features}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBox Text="{Binding LocalName}" Width="100" />
                    <CheckBox Content="IsEnabled" IsChecked="{Binding IsEnabled}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</UniformGrid>

我希望能够编辑OSPData.xaml并在运行时将其推送到应用程序。

0 个答案:

没有答案