I want to bind a Control's canvas.zindexproperty to a class property name zindex.
I tried this but it is not working
Binding zIndexBinding = new Binding("zIndex");
indicator.SetValue(Canvas.ZIndexProperty, zIndexBinding);
i have no idea how to do this, so any help will be appreciated. thank you
SetValue is to set a simple value rather than a binding. You can use instead SetBinding when using a Binding:
indicator.SetBinding(Canvas.ZIndexProperty, zIndexBinding);
Be aware that you'll need as well as the Path, which you've supplied, a Source, ElementName, or DataContext etc to resolve the object to which the path applies.
Related
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.
I'm trying to do the following using MVVM, Binding and AttachedProperty
Create a ViewObj (System.Windows.Controls.Control derived class)
The ViewObj has 1 AttachedProperty named "Order" (OrderProperty) declared in a class named View.
The attached property is bound on a property of the ViewModel in the xaml
Create the ViewModel
Set the ViewModel as DataContext of the ViewObj
Before the ViewObj is displayed/rendered/etc.
Get the order in code doing var order = View.GetOrder(ViewObj)
The ViewObj is displayed and is showing the bound value ...
If the AttachedProperty is a value and not a binding expression, the value returned by View.GetOrder(ViewObj) is the good one and not the default one.
Any ideas?
EDIT:
I forced the databinding expression to be evaluated using the BindingExpression class. I discovered that the BindingExpression.Status was set to Unattached which seems to explain why it is not working.
I think the binding is attached when the element is attached to the visual tree.
But ... that do not help me a lot with my problem ...
I discovered that (in my case at least), the Binding was Unattached, but the DataContext was set.
So I decided to get the DataContext (the ViewModel) and to work with it.
Any others suggestions are welcome.
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.
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}" />
What I have?
I have Frame in XAML, (binding works properly).
<Frame Name="frmMainContent"
DataContext="MyPageInformation"
Source="{Binding ElementName=thisPage, Path=MyPageInformation.UserControlPath}"
NavigationUIVisibility="Hidden"></Frame>
In the code behind I have a DependencyProperty of my class, PageInformation
public static DependencyProperty PageInformationProperty = DependencyProperty.Register("MyPageInformation", typeof(PageInformation), typeof(Administration));
public PageInformation MyPageInformation
{
get { retur n (PageInformation)GetValue(PageInformationProperty); }
set{ SetValue(PageInformationProperty, value); }
}
What I want?
The Frame should update its binding whenever value of MyPageInformation changes.
Can somebody tell me how I can achieve this?
Thanks in advance!
You don't have to make the PageInformationProperty a dependency property just for this binding. Implement INotifyPropertyChanged in the code behind.
Also since you are actually binding to "UserControlPath", make sure that this property actually sends change notifications.
Well, the first thing I'll tell you is that all binding errors appear on the Output window. So you need to look at it and find out if you have any errors.
The next thing.. for bindings to update automatically, you either need to make the property it is "binded" to, either a dependency property, or implement INotifyPropertyChanged for other classes.
Be sure that the property that you exactly bind to, is either one of these cases.