绑定到运行时加载的 XAML
我有一个想法,简短地解释说我想加载 xaml 文件运行时,然后将它们绑定到运行时数据。 在这些 xaml 文件中,我将使用一个名为“PropertySetter”的组件,如下所示:
public class PropertySetter : UserControl
{
public string Value { get; set; }
public string Text { get; set; }
public string Adress { get; set; }
public PropertySetter()
{
}
}
xaml 文件将如下所示:
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vf="clr-namespace:VisualFactory.vfGraphics.Graphics;assembly=VisualFactory">
<vf:PropertySetter Name="Datapoint" Text="propset" Value="false" Adress="10201" />
<CheckBox IsChecked="{Binding ElementName=Datapoint, Path=Value}" Content="{Binding ElementName=Datapoint, Path=Text}" />
</Grid>
我希望您现在明白了。 复选框会将其值(内容、ischecked)绑定到属性设置器,当应用程序运行时,它只会更改一堆“PropertySetter”的值,并且图形会更新其内容和值。 但是:虽然 xaml 在加载时正确设置了值,但当我更改属性设置器的值时,它不会更新它们。我尝试设置 Mode=TwoWay,并且属性设置器位于 iNotify 集合中。 以前有人尝试过这样的事情吗?
I had en idea, which shortly explained was that i would like to load xaml-files runtime, and then bind them to runtime data.
Inside these xaml-files i would use a component called "PropertySetter" like this:
public class PropertySetter : UserControl
{
public string Value { get; set; }
public string Text { get; set; }
public string Adress { get; set; }
public PropertySetter()
{
}
}
And the xamlfile, would look like this:
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vf="clr-namespace:VisualFactory.vfGraphics.Graphics;assembly=VisualFactory">
<vf:PropertySetter Name="Datapoint" Text="propset" Value="false" Adress="10201" />
<CheckBox IsChecked="{Binding ElementName=Datapoint, Path=Value}" Content="{Binding ElementName=Datapoint, Path=Text}" />
</Grid>
I hope you get the point by now.
The checkbox would bind its values (content, ischecked) to a propertysetter, and when the application runs, it just changes the values of a bunch of "PropertySetter"'s and the graphics update their content and values.
BUT: While the xaml correctly sets the values when it is loaded, it doesnt update them, when i change the values of the propertysetters. I have tried setting Mode=TwoWay, and the propertysetters are in a iNotify collection.
Has anyone tried something like this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里您需要做的是在
PropertySetter
类上实现INotifyPropertyChanged
并在每个属性的 setter 中触发PropertyChanged
事件。这是绑定知道在源发生更改时更新 UI 的机制。PS:对于
TwoWay
数据绑定,用于告诉数据绑定何时在目标中进行更改(在您的情况下,< code>CheckBox 的属性),这些反映在 源 中(在您的情况下,是PropertySetter
的属性)。希望这有帮助:)
What you need to do here is implement the
INotifyPropertyChanged
on yourPropertySetter
class and fire thePropertyChanged
event in the setters of each of your properties. This is the mechanism by which the binding knows to update the UI on changes to the source.PS: for the
TwoWay
databinding, that is used to tell the databinding that when changes are made in the target (in your case, theCheckBox
's properties), those are reflected in the source (in your case, thePropertySetter
's properties).Hope this helps :)
答案就在“DependencyProperty”类中。我将 PropertySetter 更改为:
瞧,一切都正确绑定,我现在在运行时加载的 XAML 和应用程序逻辑之间创建了一个层。
The answer lays in the "DependencyProperty" class. I changed the PropertySetter to this:
And voilá, it all binds correctly, and i have now made a layer in between runtime loaded XAML and app-logic.