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/2开发者_如何转开发006/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?
What you need to do here is implement the INotifyPropertyChanged
on your PropertySetter
class and fire the PropertyChanged
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.
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: for the TwoWay
databinding, that is used to tell the databinding that when changes are made in the target (in your case, the CheckBox
's properties), those are reflected in the source (in your case, the PropertySetter
's properties).
Hope this helps :)
The answer lays in the "DependencyProperty" class. I changed the PropertySetter to this:
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()
{
}
}
And voilá, it all binds correctly, and i have now made a layer in between runtime loaded XAML and app-logic.
精彩评论