binding checkbox in datatemplate to user control datacontext - silverlight

I have a listbox which include a data template of checkbox. I need the check box data context be the user control and not the listbox datacontext. How can active that in silverlight since donot have relativesource than self or template
Thanks in advance

Silverlight does not have relative source binding "out of the box", however there is a solution here that works:
http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/
You can bind your CheckBox's DataContext to that of the parent User Conntrol (In this example it is called MyUserControl) as follows:
<CheckBox>
<local:BindingHelper.Binding>
<local:BindingProperties TargetProperty="DataContext"
SourceProperty="DataContext"
RelativeSourceAncestorType="MyUserCOntrol"/>
</local:BindingHelper.Binding>
</CheckBox>
If you do not want to go down this route, you could create a ViewModel that exposes a collection of objects that you bind to your checkbox, each object could provide a reference back to the parent view model.

Related

Is it possible to bind a XAML control to a corresponding control in the ViewModel?

I am using WPF with the intent of adhering to the MVVM model. I have multiple controls in my XAML view, such as buttons, combo boxes, etc, and I have a corresponding set of controls in my ViewModel. Is there a way to bind a control (such as a button, for example) in XAML to a matching control in the ViewModel, so they have a one-to-one relationship?
I am aware of how it is usually done, with specific XAML control properties being bound to specific properties within the ViewModel. I thought that by binding an entire control to a matching control, it would simplify the binding process. I wouldn't need to bind individual properties, I could just bind the entire control and then programmatically modify any properties I wanted to in the ViewModel.
For example, my XAML button control would look something like this (the DataBinding property is not an actual property):
<Button DataBinding="SignInButton" />
And all the properties for the button would not be set in XAML, but rather in my ViewModel:
public Button SignInButton;
SignInButton.Content = "Sign In";
SignInButton.Width = 100;
SignInButton.FontFamily = "Verdana";
Expose the controls in ViewModel as ContentControl Properties and Bind them in XAML.

Retrieving value of a label in which is binded to an element value

In my current scenario (WPF, MVVM), I have a user control which hosts a visio diagram. This user control is located on a view, next to a number of labels and a datagrid element.
The user control contains a DependencyProperty object SelectedNode which value is updated with the information received from the Visio diagram. The labels' content are binded so that they display the information contained in the SelectedNode (e.g. id, name):
<Label Grid.Row="1" Grid.Column="1" x:Name="lbNodeIdValue" HorizontalAlignment="Left"
Content="{Binding ElementName=visioControlUC, Path=SelectedNode.Id, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"/>
Every time I change the selection in the diagram, the label's content changes as expected.
Next to this label, I would like to display a datagrid containing information based on the id displayed in the label. This is where I ran into problems, as I can't seem to be able to get the value of the Content property of the label in the viewmodel class.
I have tried using the MultiBinding property on the Content element of the label, and creating a second binding with Mode=OneWayToSource to set the value of the label's Content to a property I have defined in the viewmodel class.
What would be a proper way to retrieve this value in my viewmodel class?
Thanks,
Adrian
Ideally your Datagrid's ViewModel should get the value of the selected label from the other ViewModel. You should not rely on Views to transfer application data between ViewModels.
It sounds like the SelectedNode value originates from the UserControl, and not the ViewModel, so you'll need to bind the UserControl.SelectedNodeId to a ViewModel somewhere so the ViewModels have access to this data
<local:myUserControl x:Name="visioControlUC"
SelectedNode="{Binding SelectedNodeId}" />
If the value is needed by more than one ViewModel, I would highly recommend some kind of event system, such as MVVM Light's Messenger or Prism's EventAggregator. This would allow your ViewModels to subscribe to something like a SelectedNodeChangedEventMessage, and the ViewModel which actually contains the SelectedNodeId can broadcast that message anytime the value changes. You can find an example of both on my blog post about Communication between ViewModels.

how do i bind menuitem.click to a command in a different view model

I have a contextmenu, the itemsource is bound to an observable collection
i need to bind the MenuItem.Click to a command in my viewmodel..
HOW DO I DO THIS?
i have my own view model but the context menu items should be bounded to a different viewmodel..
Use RelativeSource Mode=FindAncestor to get to your parent Usercontrol and bind to the Path=DataContext.YourCommand. I believe you are trying to bind to the containing control's viewmodel.
If you dont like using RelativeSource, you can name your parent element and then using ElementName tag in the Binding extension:
If you want to bind between/cross different ViewModels, i believe you will have to use some sort of Event Broker/Aggregator. Have a look at Prism, it might give you some ideas

How to set focus on element on ItemsControl?

I've ItemsControl that its ItemsSource property bind to some dictionary from code behind. The ItemTemplate is consist of only one button. So, for each item in dictionary it creates button.
My question is how can I set focus to one of these buttons (dynamically)?
Should I use ItemContainerGenerator.ContainerFromItem ?
Any other idea?
Thanks in advance!
Yes, use can ItemContainerGenerator.ContainerFromItem to get a container for your data item, then you will need to find you button inside this container and call Focus() on the button.
OR you can use an attached property to bind IsFocused to a property on your data item. See Set focus on textbox in WPF from view model (C#)

WPF USer Control viewmodel binding

HI Can any one suggest me how bind viewmodel to a usercontrol..
Also please share different way of doing that..
I have added viewmodel and view into my xaml file in namespace and in the user control resource tag.. i have defined a data template with data type as the viewmodel wh i have wrote.. inside that i have added my view (i mean the same usercontrol ih which im editing now is it possible -- please let me know).. I have used content control with content={Binding}.. and contenttemplate as a datatemplate.. in that i have reffered the property which i want to bind from viewmodel).. but its not binding as such..
My query is different ways of binding viewmodel to view in UserControlLibrary Project ?
Sdry's right, you can also set the DataContext property of your view to your ViewModel.
Also, here's a WPF databinding cheat sheet that will likely come in handy: http://www.nbdtech.com/Free/WpfBinding.pdf
Set the datacontext of your view, with an instance of your viewmodel. Throughout your view you can then bind to the properties of your viewmodel with "Path" in the binding.

Resources