As you can see in code below, I have descendant of ScrollViewer, and set Name of MyScrollView in ctor. But when I try using MyScrollViewer in control template I cannot find one via Template.FindName
If I change <local:MyScrollViewer /> to <local:MyScrollViewer Name=""PART_ContentHost"" /> code works as expected, but I am looking for solution without changing XAML.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var parserContext = new ParserContext
{
XamlTypeMapper = new XamlTypeMapper(new string[0]),
XmlnsDictionary =
{
{ "", "http://schemas.microsoft.com/winfx/2006/xaml/presentation" },
{ "x", "http://schemas.microsoft.com/winfx/2006/xaml" },
{ "local", "clr-namespace:" + typeof(MyScrollViewer).Namespace + ";assembly=" + typeof(MyScrollViewer).Assembly.FullName}
}
};
var template = (ControlTemplate)XamlReader.Parse(#"
<ControlTemplate TargetType=""TextBox"">
<Border>
<local:MyScrollViewer />
<!--<local:MyScrollViewer Name=""PART_ContentHost""/> -->
</Border>
</ControlTemplate>
", parserContext);
// Name=""PART_ContentHost""
Content = new MyTextBox { Template = template };
}
}
public class MyScrollViewer: ScrollViewer
{
public MyScrollViewer() => Name = "PART_ContentHost";
}
public class MyTextBox: TextBox
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var contentHost = Template.FindName("PART_ContentHost", this);
if (contentHost == null)
throw new Exception("Can not find PART_ContentHost");
}
}
updated:
Even I put my control template into MainWindow.xaml (and remove from MainWindow ctor), it doesnot work.
public MainWindow()
{
InitializeComponent();
}
<Window x:Class="WpfApp1.MainWindow"
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:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.Resources>
<ControlTemplate TargetType="local:MyTextBox" x:Key="TextBoxTemplate">
<local:MyScrollViewer />
</ControlTemplate>
</Grid.Resources>
<local:MyTextBox Template="{StaticResource TextBoxTemplate}" />
</Grid>
</Window>
The constructor of MyScrollViewer will not be called on template creation with XamlReader, so the names with which MyScrollViewer was created are stored in internal dictionaries. See template.ChildNames. The constructor of MyScrollViewer will first being called, when MyTextBox becomes visible, but it's already too late.
Template being created from XAML and it notices the children names by parsing, without creation of children instances. Later the children instances will be created, but the template hold old names. So if you call Template.FindNames with new names, they will not be found.
Try
var contentHost = Template.FindName("2_T", this);
but I am looking for solution without changing XAML
Setting the name in the constructor of the ScrollViewer won't work. You have to set it in the template for it to be registered in the namescope of the template.
If you don't want to assign the element a Name in the template, you could wait for it to get created and then find it in the visual tree without using a name:
public class MyTextBox : TextBox
{
public MyTextBox()
{
Loaded += MyTextBox_Loaded;
}
private void MyTextBox_Loaded(object sender, RoutedEventArgs e)
{
MyScrollViewer sv = FindVisualChild<MyScrollViewer>(this);
//...
}
private static T FindVisualChild<T>(Visual visual) where T : Visual
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
{
Visual child = (Visual)VisualTreeHelper.GetChild(visual, i);
if (child != null)
{
T correctlyTyped = child as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
T descendent = FindVisualChild<T>(child);
if (descendent != null)
{
return descendent;
}
}
}
return null;
}
}
Related
I would like to implement a DataGrid that displays data of objects with the same class. A list, which accepts generic type of class, of objects called objectsList and a list of string of properties called propertiesToDisplay are provided so that the DataGrid can choose which properties of the objects to be shown, according to propertiesToDisplay. How can I implement it?
MainWindow
<Window x:Class="SomeProject.MainWindow"
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:local="clr-namespace:SomeProject"
mc:Ignorable="d"
Title="MainWindow" MinHeight="450" MinWidth="700">
<Grid>
<DataGrid ItemsSource="{Binding ObjectsList}">
<!-- Anything Else? -->
</DataGrid>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// How is the binding?
}
}
ViewModel
public class ViewModel<T>
{
private IList<T> _objectsList;
public IList<T> ObjectsList
{
get
{
return _objectsList;
}
set
{
_objectsList = value;
}
}
private IList<string> _propertiesToDisplay;
public IList<string> PropertiesToDisplay
{
get
{
return _propertiesToDisplay;
}
set
{
_propertiesToDisplay= value;
}
}
// Constructor
public ViewModel(IList<T> objectsList, IList<string> propertiesToDisplay)
{
// Please help me to correct the following code
PropertyInfo[] propertyInfos = null;
foreach (var propertyName in propertiesToDisplay)
{
propertyInfos = typeof(T).GetProperties();
var names = propertyInfos.Select(x => x.Name);
if (!names.Contains(propertyName))
{
throw new ArgumentException("");
}
}
try
{
ObjectsList = objectsList;
foreach (var obj in objectsList)
{
foreach (var propertyName in propertiesToDisplay)
{
PropertiesToDisplay.Add(propertyInfos.Where(x => x.Name.Equals(propertyName)).FirstOrDefault().GetValue(obj, null).ToString());
}
}
}
catch (Exception ex)
{
}
}
}
You could create the columns dynamically in the view based on the PropertiesToDisplay source property. Something like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel<YourClass> viewModel = new ViewModel<YourClass>(..., ...);
foreach (string column in viewModel.PropertiesToDisplay)
dataGrid.Columns.Add(new DataGridTextColumn() { Header = column, Binding = new Binding(column) });
dataGrid.AutoGenerateColumns = false;
DataContext = viewModel;
}
}
XAML:
<DataGrid x:Name="dataGrid" ItemsSource="{Binding ObjectsList}" />
Problem
A user WPF control is made up of multiple standard controls.
How can multiple dependency properties of the component (base or standard) controls be accessed in XAML, when implementing the parent (user) control, without creating additional properties?
Details
What do I mean by "creating additional dependency properties"? Well, that is the only way I know of accessing properties of the component controls: by implementing attached properties, as described at MSDN here.
However, it presents the following problems:
Existing dependency properties must be copied as new properties, defeating the DRY principle.
If data binding is to occur, more work must be done to bind existing dependency properties to the new exposed dependency properties.
I'm wondering if there is a way to "walk" the base controls within the user control, to access their properties - from within XAML.
Example
For example, I make a user WPF control that inherits from UserControl. It is simple - it consists of a StackPanel containing a Label and a TextBlock:
<UserControl x:Class="MyApp.CustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<Label Name="BaseLabel">Label Here</Label>
<TextBlock Name="BaseTextBlock">Some text here.</TextBlock>
</StackPanel>
</UserControl>
Now, when I use my UserControl elsewhere in XAML, I'm wishfully thinking something like this could be done to edit my Label's content... although I don't know of a way:
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp">
<StackPanel>
<!-- This won't work, don't try at home kids. -->
<local:CustomControl BaseLabel.Content="I did it!"></local:CustomControl>
</StackPanel>
</Window>
Much thanks.
How about the next solution:
1. Create the AttachedProperty (because you must an entry point) and bind this property to the collection of data.This collection of data will contain changes you want perform on sub-controls of a main user control used inside the window. This collection will be defined inside the main window view model.
2. In attached property changed callback get the binded collection, parse it data into sub-controls properties.
Here is the solution:
3. Xaml code:
<Window.DataContext>
<nirHelpingOvalButton:MainWindowViewModel />
</Window.DataContext>
<Grid>
<nirHelpingOvalButton:InnerControl x:Name="MyInnerControl"
nirHelpingOvalButton:Helper.InnerControlPropertiesAccessor="{Binding InnerData, Mode=Default, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
4. Attached property code (bindig support):
public static readonly DependencyProperty InnerControlPropertiesAccessorProperty = DependencyProperty.RegisterAttached(
"InnerControlPropertiesAccessor", typeof (ObservableCollection<TargetControlData>), typeof (Helper), new PropertyMetadata(default(ObservableCollection<TargetControlData>), InnerValueAccessProviderPropertyChangedCallback));
public static void SetInnerControlPropertiesAccessor(DependencyObject element, ObservableCollection<TargetControlData> value)
{
element.SetValue(InnerControlPropertiesAccessorProperty, value);
}
public static ObservableCollection<TargetControlData> GetInnerControlPropertiesAccessor(DependencyObject element)
{
return (ObservableCollection<TargetControlData>) element.GetValue(InnerControlPropertiesAccessorProperty);
}
private static void InnerValueAccessProviderPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
var control = sender as Control;
if (control == null) return;
var valuesMap = args.NewValue as ObservableCollection<TargetControlData>;
if (valuesMap == null)
return;
valuesMap.ToList().ForEach(data => TryToBind(control, data));
}
private static void TryToBind(Control control, TargetControlData data)
{
var innerControl = control.FindName(data.SubControlName) as DependencyObject;
if (innerControl == null) return;
var myBinding = new Binding
{
Source = data,
Path = new PropertyPath("Data"),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
var descriptors = TypeDescriptor.GetProperties(innerControl);
var propertyDescriptor = descriptors.Find(data.SubConrolProperty, true);
var descriptor = DependencyPropertyDescriptor.FromProperty(propertyDescriptor);
if (descriptor == null) return;
var dependencyProperty = descriptor.DependencyProperty;
BindingOperations.SetBinding(innerControl, dependencyProperty, myBinding);
}
5. Inner control xaml:
<UserControl x:Class="NirHelpingOvalButton.InnerControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UniformGrid>
<Button x:Name="InnerControlButton"></Button>
<TextBlock x:Name="InnerContentTextBlock"></TextBlock>
</UniformGrid>
6. ViewModel code:
public class MainWindowViewModel:BaseObservableObject
{
private static int _staticCount = 0;
private List<Brush> _list = new List<Brush> {Brushes.Green, Brushes.Red, Brushes.Blue};
public MainWindowViewModel()
{
InnerData = new ObservableCollection<TargetControlData>
{
new TargetControlData
{
SubControlName = "InnerControlButton",
SubConrolProperty = "Content",
Data = "Click Me",
},
new TargetControlData
{
SubControlName = "InnerControlButton",
SubConrolProperty = "Command",
Data = new RelayCommand(CommandMethod),
},
new TargetControlData
{
SubConrolProperty = "Text",
SubControlName = "InnerContentTextBlock",
Data = "Hello"
},
new TargetControlData
{
SubConrolProperty = "Background",
SubControlName = "InnerContentTextBlock",
Data = Brushes.Green
},
new TargetControlData
{
SubConrolProperty = "Foreground",
SubControlName = "InnerContentTextBlock",
Data = Brushes.White
},
};
}
private void CommandMethod()
{
_staticCount ++;
var backgroundData = InnerData.FirstOrDefault(data => data.SubControlName == "InnerContentTextBlock" && data.SubConrolProperty == "Background");
var textData = InnerData.FirstOrDefault(data => data.SubControlName == "InnerContentTextBlock" && data.SubConrolProperty == "Text");
if (backgroundData == null || textData == null) return;
var index = _staticCount%_list.Count;
backgroundData.Data = _list[index];
textData.Data = string.Format("{0} {1}", "Hello", backgroundData.Data);
}
public ObservableCollection<TargetControlData> InnerData { get; set; }}
7. TargetControlData code:
public class TargetControlData:BaseObservableObject
{
private string _subControlName;
private string _subConrolProperty;
private object _data;
public string SubControlName
{
get { return _subControlName; }
set
{
_subControlName = value;
OnPropertyChanged();
}
}
public string SubConrolProperty
{
get { return _subConrolProperty; }
set
{
_subConrolProperty = value;
OnPropertyChanged();
}
}
public object Data
{
get { return _data; }
set
{
_data = value;
OnPropertyChanged();
}
}
}
Summary - you can pull control properties data from configuration file, or collect them by reflection.
regards,
The way you suggested - I don't think this would be possible.
But it can be done with normal properties, instead of dependency properties, something like:
UserControl xaml:
<StackPanel>
<TextBlock x:Name="tbOne"></TextBlock>
<TextBlock x:Name="tbTwo" Foreground="Red"></TextBlock>
</StackPanel>
UserControl code behind:
public string One
{
get
{
return this.tbOne.Text;
}
set
{
this.tbOne.Text = value;
}
}
public string Two
{
get
{
return this.tbTwo.Text;
}
set
{
this.tbTwo.Text = value;
}
}
and the usage of user control:
<local:UserControl1 One="test1" Two="test2"></local:UserControl1>
I have a User Control Library that I am loading dynamically. From that lib I am inserting a Tabitem into a TabControl. I can load the tab and show it without error. However, I can't seem to get the binding on the control working.
This is the code I use to load it and add it to the TabControl:
Assembly moduleAssembly = Assembly.Load("ControlLib");
UserControl uc = (UserControl)Application.LoadComponent(new System.Uri("/ControlLib;component/UserControl1.xaml", UriKind.RelativeOrAbsolute));
TabControl itemsTab = (TabControl)this.FindName("mainTabControl");
TabItem newTab = new TabItem();
newTab.Content = uc;
newTab.Header = "Test";
itemsTab.Items.Add(newTab);
itemsTab.SelectedItem = newTab;
This is the C# code for the control:
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty TestStringProperty =
DependencyProperty.Register("TestString", typeof(string), typeof(UserControl1));
public string TestString { get; set; }
public UserControl1()
{
InitializeComponent();
TestString = "Hello World";
}
}
This is the XAML code for the control:
<UserControl x:Class="ControlLib.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Height="30" Width="100" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path=TestString, Mode=TwoWay}" />
</Grid>
</UserControl>
When the tab displays all I see if a blank in the TextBox rather than "Hello World"
What am I missing?
You would still be setting the DataContext of your user control to instance of the class. Just how you go about creating that instance differs as you would be loading that dll a runtime. But fundamentally the binding setup remains the same.
var assembly = Assembly.LoadFrom(#"yourdllname.dll");
Type type = assembly.GetType("ClassLibrary1.SampleViewModel");
object instanceOfMyType = Activator.CreateInstance(type);
DataContext = instanceOfMyType;
For how basic databinding works read MSDN documentation.
Make sure you select the correct framework on the top of the screen.
EDIT
Usually this is created as a separate class (ViewModel in MVVM pattern).
public partial class Window3 : Window, INotifyPropertyChanged
{
public Window3()
{
InitializeComponent();
DataContext = this;
TestString = "Hello World.";
}
string _testString;
///<summary>Gets or sets TestString.</summary>
public string TestString
{
get { return _testString; }
set { _testString = value; OnPropertyChanged("TestString"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
var e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}
}
I have a screen with several UserControls, but only one of them remains active. The other UserControls aren't shown, but the user can switch the active flag of any of those who are not active. One of the UserControl contains an ItemsControl.
I need to know all the controls in the view, including those generated by an ItemsControl, after loading the first UserControl that is active in the screen, when view is finally initialized.
For ItemsControl, wpf didn't instance any item until it was painted on the screen that contains the UserControl (so I've tried, until the Load event is launched), so that I can't found the controls contained by the view because it didn't exist.
Is there any way to change this behavior?
I try to change the value of property VirtualizingStackPanel.IsVirtualizing to false, to avoid the previous behaviour, with no success. To illustrate this, I write this view example:
<Window x:Class="ContenidoEnTabs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="spContainer" Orientation="Vertical" VirtualizingStackPanel.IsVirtualizing="False">
<Button Content="Push" Click="Button_Click" />
</StackPanel>
</Window>
This view creates a second control not visible until the user press the button:
public partial class MainWindow : Window
{
private NotPaintedOnInitUserControl controlExtra;
public MainWindow()
{
InitializeComponent();
controlExtra = new NotPaintedOnInitUserControl();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
spContainer.Children.Add(controlExtra);
}
}
The control not visible initially is as follow:
<UserControl x:Class="ContenidoEnTabs.NotPaintedOnInitUserControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ItemsControl ItemsSource="{Binding MyCollection}" x:Name="itemsControlTarget"
VirtualizingStackPanel.IsVirtualizing="False">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox x:Name="aTextBox" Width="80" Initialized="ATextBox_Initialized" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>
and in CodeBehind I detect when the Items were created
public partial class NotPaintedOnInitUserControl : UserControl
{
public NotPaintedOnInitUserControl()
{
InitializeComponent();
DataContext = new SimpleListDataContext();
}
private void ATextBox_Initialized(object sender, EventArgs e)
{
}
}
And the DataContext used:
public class SimpleListDataContext
{
private List<string> _myCollection;
public List<string> MyCollection
{
get { return _myCollection ?? (_myCollection = new List<string> { "one", "two" }); }
set { _myCollection = value; }
}
}
Any ideas?
Thanks in advance.
If you want WPF to generate the tree for a control that isn't part of the view, you can "hydrate" and layout the control by forcing the layout to run. Something like this should work:
public partial class MainWindow : Window
{
private NotPaintedOnInitUserControl controlExtra;
public MainWindow()
{
InitializeComponent();
controlExtra = new NotPaintedOnInitUserControl();
// Force the control to render, even though it's not on the screen yet.
var size = new Size(this.Width, this.Height);
var rect = new Rect(new Point(0,0), size);
controlExtra.Measure(size);
controlExtra.Arrange(rect);
controlExtra.InvalidateVisual();
controlExtra.UpdateLayout();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
spContainer.Children.Add(controlExtra);
}
}
Not sure if this is what you're asking. If not, please clarify paragraph 2.
Have a look at LogicalTreeHelper.GetChildren(myUiElement)
This looks at the logical tree rather than the visual tree so it examines the structure without needing to have loaded the control to get the visual structure
In the below control to find is the name of the contorl i.e. myDatagrid
You could also adapt this to just get all the children of a particular control i.e.
FindChildInVisualTree(this, "mydatagrid"); // assumming this a UIElement (i.e. your in the code behind)
find the control using the below then using LogicalTreeHelper get all it's children.
public static UIElement FindChildInVisualTree(UIElement view, string controlToFind)
{
UIElement control = null;
try
{
if (view != null)
{
if ((view as FrameworkElement).Name.ToUpper() == controlToFind.ToUpper())
{
control = view;
}
else
{
DependencyObject depObj = view as DependencyObject;
if (depObj != null)
{
foreach (var item in LogicalTreeHelper.GetChildren(depObj))
{
control = FindChildInVisualTree(item as UIElement, controlToFind);
if (control != null)
{
break;
}
}
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("Error finding child control: " + controlToFind, ex);
}
return control;
}
I have a custom control and a view model object. A property on the view model is bound to the custom control and I can see that the custom control actually receives the vaule from the view model object - yet my handler code (GeometryText.Set) is not executed. What am I doing wrong?!
Notice the event handlers on custom control where I've placed breakpoints- if I change the size of the window, I can inspect the GeometryText property in the watch window - and it's clearly updated in the cases where I expect it to.
Thanks for any input,
Anders, Denmark
ComponentDrawing.xaml.cs
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Rap1D.ServiceLayer.Interfaces.Services;
using StructureMap;
namespace Rap1D.Rap1D_WPF.Controls
{
/// <summary>
/// Interaction logic for ComponentDrawing.xaml
/// </summary>
public partial class ComponentDrawing
{
public static DependencyProperty GeometryTextProperty =DependencyProperty.Register("GeometryText", typeof (string), typeof (ComponentDrawing), new FrameworkPropertyMetadata
(
"",
FrameworkPropertyMetadataOptions
.
None));
private Canvas _canvas;
public ComponentDrawing()
{
InitializeComponent();
}
public string GeometryText
{
get { return ((string) GetValue(GeometryTextProperty)); }
set
{
SetValue(GeometryTextProperty, value);
ReadGeometryTextIntoDrawing(value);
}
}
private void ReadGeometryTextIntoDrawing(string fileText)
{
// Allow control to be visible at design time without errors shown.
// I.e. - don't execute code below at design time.
if (DesignerProperties.GetIsInDesignMode(this))
return;
// OK - we are a running application
//if (_canvas != null)
// return;
// OK - this is first time (-ish) we are running
if (ActualWidth == 0)
return;
// We have a valid screen to pain on
var componentDrawingService = ObjectFactory.GetInstance<IComponentDrawingService>();
//var commandTextProvider = ObjectFactory.GetInstance<ICommandTextProvider>();
//var fileText = ((IViewModelBase) DataContext).GeometryText;
// If getting the file text fails for some reason, just abort to avoid further problems.
if (fileText == null)
return;
var pg = componentDrawingService.GetDrawings(fileText, 0, ActualWidth, 0, ActualHeight);
_canvas = new Canvas();
foreach (var path in pg)
{
_canvas.Children.Add(path);
}
Content = _canvas;
}
private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
//ReadGeometryTextIntoDrawing();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
//ReadGeometryTextIntoDrawing();
}
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
//ReadGeometryTextIntoDrawing();
}
}
}
ComponentDrawing.xaml:
<UserControl x:Class="Rap1D.Rap1D_WPF.Controls.ComponentDrawing" 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" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
DataContextChanged="UserControl_DataContextChanged" Loaded="UserControl_Loaded" SizeChanged="UserControl_SizeChanged">
<Grid Background="White">
<Path Stroke="Black"></Path>
</Grid>
</UserControl>
Usage:
<Controls:RadPane x:Class="Rap1D.Rap1D_WPF.Controls.ProductComponentDetails" 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:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking" xmlns:Controls1="clr-namespace:Rap1D.Rap1D_WPF.Controls" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Header="{Binding DisplayName}">
<Controls1:ComponentDrawing GeometryText="{Binding GeometryText}" />
</Controls:RadPane>
view model object (implementing INotifyPropertyChanged):
using System;
using Microsoft.Practices.Prism.Events;
using Rap1D.ExternalInterfaceWrappers.Interfaces;
using Rap1D.ModelLayer.Interfaces.Adapters;
using Rap1D.ModelLayer.Interfaces.Structure;
using Rap1D.ServiceLayer.Interfaces.Adapters;
using Rap1D.ServiceLayer.Interfaces.Providers;
using Rap1D.ViewModelLayer.Interfaces;
namespace Rap1D.ViewModelLayer.Implementations
{
public class ProductComponentViewModel : TreeViewItemViewModel, IProductComponentViewModel
{
...
public override string GeometryText
{
get
{
var pentaResponse = _commandTextProvider.GetCommandText(ProductComponent);
return DateTime.Now.ToString()+ pentaResponse.Payload;
}
}
...
}
}
Dependency property setters are not invoked if changed by binding. If you want to somehow react on dependency property value changing you should register a callback in property metadata:
http://msdn.microsoft.com/en-us/library/ms557294.aspx
something like that (not sure it is compilable, let me know if something wrong):
public static DependencyProperty GeometryTextProperty =
DependencyProperty.Register(... , new FrameworkPropertyMetadata(GeometryTextCallback));
public static void GeometryTextCallback(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
// cast source to your type and invoke method from your setter
((ComponentDrawing)source)ReadGeometryTextIntoDrawing(value);
}