Unable to access property of a nested UserControl - wpf

I have one UserControl nested inside the other UserControl, something like that:
<uc:MyControl1>
<uc:MyControl2 Name="cheese"/>
</uc:MyControl2>
uc:MyControl2 contains a DP Name.
And inside uc:MyControl1 I am exposing uc:MyControl2 using a standard property:
public uc:MyControl1 ExposedMyProperty1 {get{return MyProperty1}}
But I still can't access it form the uc:MyControl1 :
<uc:MyControl1 ExposedMyProperty1.Name="Milk">
<uc:MyControl2/>
</uc:MyControl2>
Intellisense shows only the properties of the UserControl, but not of my derived version of the UserControl. Meaning I see all the properties which comes from the UserControl, but don't see DP I have defined. So it looks like I am getting a UserControl instead of uc:MyControl1.
Am I missing something here?

In your code sample you only expose a getter and not a setter. That may be your problem.
Try this:
public string ExposedMyProperty1 { get {return MyProperty1; } set {MyProperty1 = value;}}
But your code sample appears to be incomplete. For you say you are exposing a DependencyProperty, and your property is only a regular property. So this may not be the solution to your problem.

I have found an answer to my question. The answer is: you can't access nested properties from you xaml. I don't know what is the reason for WPF not allowing that..
The answer comes from this post

Related

How to set my custom class's member View's property from XAML? (Xamarin.forms)

I'm making app with using Xamarin.forms.
I already asked question here.
How to set child of class' property with using xaml? (Xamarin.forms)
But I couldn't get right answer for this, or there may be no solution for that.
What I want to do is setting my class's view's property from ContentPage's XAML.
my class has some view like Image and else.
I searched and found that there is 'ControlTemplete'. But I'm not sure it's what I'm looking for.
And I also don't think putting BindableProperty and OnPropertyChangedDelegate codes for every property that I want to set is a best way.
Is there another better solution?
Thanks.
You can map XAML that is inside your control to a property using ContentProperty attribute.
[ContentProperty("MyContent")]
public class MyControl : ContentView
{
public View MyContent { get; set; }
}
And in XAML somthing like this
<local:MyControl>
<Grid></Grid>
</local:MyControl>
this limits you to only one property but should work with any types.

Add DependencyObject to control from code-behind

I want to add a DependencyObject to a control from code behind. I have searched and searched online for how to do this with absolutely no success. The DependencyObject has a DependencyProperty. I also want to set this property from code-behind.
charting:ChartBehaviors inherits directly from DependencyObject. It is a class I wrote myself. The Chart control is a Third-party control.
charting:ChartBehaviors.FloatingTooltip is the DependencyProperty. This is also a class I wrote myself.
Here is what it looks like in XAML. I want to do this in code behind so that I can turn on and off the "behavior".
<charting:Chart>
<charting:ChartBehaviors.FloatingTooltip>
<charting:FloatingTooltipBehavior
TooltipTemplate="{StaticResource tooltipTemplate}" />
</charting:ChartBehaviors.FloatingTooltip>
</charting:Chart>
You can just use the SetValue method on the object (assuming you give your chart an id of chart1).
var behave = new FloatingTooltipBehavior();
chart1.SetValue(ChartBehaviors.FloatingTooltipProperty, behave);
Another solution would be to just add an Enabled property to your behavior and then set that from the code behind.
Well, you basically instantiate necessary dependency object and use appropriate method to add it to the control. For example, to add TextBlock to the StackPanel, you write it like this:
TextBlock txtMyText = new TextBlock();
stackPanel.Children.Add(txtMyText);
If you're wanting to add certain behavior to a chart, you should just get your chart object in code and look for a property like Behaviors or something. Then you either assign a behavior (if it's one-behavior-only) or add it like to the stackpanel:
Chart myChart;
myChart.Behavior = new FloatingTooltipBehavior();
It's hard to tell the exact syntax without knowing the component.

Can I have a MVVM model inherited from an other model?

I have a ProductViewModel class which contains different properties.
Then I have a ProductDetailsViewModel class which inherit from ProducViewModel class. The reason I am doing it this way is in order to get correct binding environement and avoid duplication of properties from previous view.
I am allowed to do this or each ViewModel should be clearly isolated?
Through code I can acess the properties of the ProductViewModel class from ProductDetailsViewModel view but when I set the datacontext of my ProductDetailView to ProducDetailsViewModel class and bind properties URI for instance which is define inside the inherited class, binding seems not occurs.
Any idea ?
You can do this too, but i think maybe better would be to separate them and use Dependency Injection.
You create and interface for your ProductViewModel and implement it and then you inject this into your ProductDetailsViewModel.
MVVM + WPF + DI
MSDN DI
Yes this is fine, and I do this all the time in my WPF projects so it should just work. Some suggestions:
Can you check your output window when debugging the application. Are there any binding errors suggesting a mis-typed xaml binding?
Are you using any DataTemplates in xaml which bind to a specific type, e.g. ProductViewModel not ProductDetailsViewModel?
Does the base type (ProductViewModel) implement INotifyPropertyChanged?
Are all the properties in ProductViewModel and ProductSetailsViewModel raising the PropertyChanged event with directly typed string property name?
Best regards,

How to set the default binding converter in Wpf?

I'm moving project from Silverlight to WPF and I've come across a problem.
I have a control with an INotifyPropertyChanged property GeoRect of type GeoRect. GeoRect has a variety of public properties that are set in its constructor each of type IGeoPosition.
I am setting a binding to one of these properties like so:
<TextBlock Text="{Binding GeoRect.TopRight, ElementName=x_SomeControl}"></TextBlock>
In Silverlight the default ToString method is called on IGeoPosition instance every time the GeoRect property changes. In Wpf I don't get any text at all.
I can correct this in Wpf by adding a ValueConverter to the TextBlock which simply calls the ToString method on the object, but this appears to be unnecessary fat. Can anyone help?
I suspect that there is another problem in your binding. Also in WPF, data binding calls the ToString() method to build the text of a Text-control.
Have you checked the output window of visual studio for a binding error? Or maybe the GeoRect-class does not support INotifyPropertyChanged for the TopRight property?
I guess that ElementName=x_SomeControl and GeoRect.TopRight are causing a probable "Source and Path" comination error. Are you sure your x_SomeControl has a property called 'GeoRect'? Also is x_SomeControl.GeoRect not null? And x_SomeControl.GeoRect.TopRight has a correct value?
As HCL pointed out, this will become apparent when you view your Output window where BindingExpression error must have appeared for this binding.
Please check.

Execute a usercontrol command from a viewmodel

I have a usercontrol with a command, what I would like to do is execute this command from the containing view's ViewModel.
This would be easy to accomplish in the code behind, as I could just go "UserControl.MyCommand.Execute", but of course I want to be able to do this in the ViewModel.
In theory, what I would like to do is bind the UserControl's Command to a Command on the ViewModel which I can execute and will then be handled by the UserControl. Something like this:
...
<local:MyControl
MyCommand="{Binding ViewModelsCommand}" />
...
Of course this will have the opposite affect to what I want to do, as now the ViewModelsCommand is bound to MyCommand. So how to invert this?
Basically I want to be able to bind something like this:
ViewModelsCommand="{Binding MyControl.MyCommand}"
Any ideas or inspiration would be welcomed, I can't see a binding Mode that would let me do this. And I'm not sure how to access the DataContext's properties for binding (usually you would do just bind and have twoway handle this, but of course that doesn't work in this scenario).
Thanks in advance.
You are instantiating the view-model in the constructor of the view.
Why not set the value explicitly upon construction?
public SomeView()
{
var viewModel = new SomeViewModel();
viewModel.ViewModelCommand = MyCommand; // or = myControl.MyCommand
DataContext = viewModel;
}
It is possible to use a binding with OneWayToSource, TwoWay, or Explicit, but you still have to explicitly update source at least once in code (always if you use Explicit).
myControl.GetBindingExpression(MyControl.MyCommandProperty).UpdateSource();
I use PRISM's EventAggregator, or MVVMLight's Messenger to allow two ViewModels to talk, but your case looks slightly different where you have a view(UserControl) talking to a ViewModel.
Please note, the following answer is not correct. It seems that OneWayToSource only updates after the target-property has changed. However I don't delete this answer to inform other people which are not aware of this behaviour (like me).
Old answer (see text above)
IMO your example should work (if MyControl.MyCommand is a public property that returns an ICommand). Have you tried the BindingMode OneWayToSource?
<local:MyControl
MyCommand="{Binding ViewModelsCommand,Mode=OneWayToSource}" />

Resources