运行时XAML wpf中的绑定
我试图在XAML中存储一些数据,并在运行时加载它。用于存储我的xaml,如下所示运行时XAML wpf中的绑定,wpf,binding,wpf-controls,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"
<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准备好之后,我存储并在运行时加载该类以填充我的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;
StringReader-StringReader=newstringreader(File.ReadAllText(“Model/OSPData.xaml”);
XmlReader=XmlReader.Create(stringReader);
var data=(OSPData)XamlReader.Load(xmlReader);
SimpleIoc.Default.GetInstance().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并在运行时将其推送到应用程序。您是否设置了datacontext?您使用的是mvvmlight吗?是的,我使用的是mvvmlight,并且datacontext设置为LocalName的绑定正在工作仅使用元素名称的绑定不工作获取解决方案,添加时需要调用AddLogicalChild
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;
<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>