Wpf 绑定到运行时加载的XAML
我有一个想法,这很快解释了我想在运行时加载xaml文件,然后将它们绑定到运行时数据。 在这些xaml文件中,我将使用名为PropertySetter的组件,如下所示:Wpf 绑定到运行时加载的XAML,wpf,xaml,data-binding,runtime,Wpf,Xaml,Data Binding,Runtime,我有一个想法,这很快解释了我想在运行时加载xaml文件,然后将它们绑定到运行时数据。 在这些xaml文件中,我将使用名为PropertySetter的组件,如下所示: public class PropertySetter : UserControl { public string Value { get; set; } public string Text { get; set; } public string Adress { get; set; } pub
public class PropertySetter : UserControl
{
public string Value { get; set; }
public string Text { get; set; }
public string Adress { get; set; }
public PropertySetter()
{
}
}
<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>
xamlfile的外观如下所示:
public class PropertySetter : UserControl
{
public string Value { get; set; }
public string Text { get; set; }
public string Adress { get; set; }
public PropertySetter()
{
}
}
<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>
我希望你现在明白了。
复选框将其值绑定到内容,并被选中到propertysetter,当应用程序运行时,它只更改一组propertysetter的值,图形将更新其内容和值。
但是:虽然xaml在加载时正确地设置了值,但当我更改propertysetters的值时,它不会更新这些值。我已经尝试过设置Mode=TwoWay,并且PropertySetter位于iNotify集合中。
以前有人尝试过类似的方法吗?这里需要做的是在PropertySetter类上实现INotifyPropertyChanged,并在每个属性的setter中激发PropertyChanged事件。这是绑定知道在源代码发生更改时更新UI的机制
class PropertySetter :UserControl,INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string address;
private string text;
private string value;
public string Value
{
get { return value; }
set
{
if (value == value)
return;
value = value;
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
}
public string Text
{
get { return text; }
set
{
if (text == value)
return;
text = value;
PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}
}
public string Address
{
get { return address; }
set
{
if (address == value)
return;
address = value;
PropertyChanged(this, new PropertyChangedEventArgs("Address"));
}
}
public PropertySetter()
{
}
}
PS:对于双向数据绑定,它用于告诉数据绑定,当您的案例中的目标发生更改时,复选框的属性,这些属性反映在案例中的源中,即PropertySetter的属性
希望这有帮助:答案在于DependencyProperty类。我将PropertySetter更改为:
public class PropertySetter : UserControl
{
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(string), typeof(PropertySetter), new UIPropertyMetadata("0"));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(PropertySetter), new UIPropertyMetadata("def"));
public string Adress
{
get { return (string)GetValue(AdressProperty); }
set { SetValue(AdressProperty, value); }
}
public static readonly DependencyProperty AdressProperty =
DependencyProperty.Register("Adress", typeof(string), typeof(PropertySetter), new UIPropertyMetadata(""));
public PropertySetter():base()
{
}
}
瞧,所有绑定都是正确的,我现在在运行时加载的XAML和应用程序逻辑之间创建了一个层 嗨,AbdouThank,谢谢你的回答,但是我已经试过了,但是没有用:但是我在这里找到了答案:我只需要在propertysetter类中使用DependencyProperty。哦!是的,这将是我的第一个回答,这确实是一个问题,因为您正在用XAML设置值,但后来我读了这个问题,忘记了这一点,只看到您没有执行更改通知。但是,我知道为什么需要在XAML中创建PropertySetter,我也要添加这句话