WPF Binding Button.Command in ControlTemplate to property of ViewModel - binding

My ViewModel has a property called Commands which is of type IDictionary.
For my data grid I have created a ControlTemplate for one of the fields using a button as follows:
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Button Style="{DynamicResource btnRemove}" Width="14" Height="14"
Content="{TemplateBinding Content} "
CommandParameter="{Binding ViewID}"
Command="{Binding Commands[AcknowledgeErrorCmd]}" />
<ControlTemplate.Triggers>
</ControlTemplate.Triggers>
</ControlTemplate>
Clicking on the button does nothing which tells me the binding did not work. However, an unstyled button added to the toolbar of the same window hosting this grid works, binds properly to this command. I guess my question is:
Hw do I bind the command property of a button used in a ControlTemplate to a ViewModel?
TIA.

I am not sure what the problem is but try to debug your solution and look into the output window with Debug selected in the combobox and you will see the errors that occur during binding. Maybe this will help you to the solution.
Provide me the error as a comment on this post if you don't understand it.

I did this instead:
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Border >
<TextBlock Margin="5">
<Hyperlink
CommandParameter="{Binding ElementName=root, Path=DataContext.ViewID}"
Command="{Binding ElementName=root, Path=DataContext.Commands[AcknowledgeErrorCmd]}">
<TextBlock Text="Acknowledge"/>
</Hyperlink>
</TextBlock>
</Border>
</ControlTemplate>
and that works fine. It may be related to the post Viko provided.

Related

How to binding to relative source

Do you see any problem with my codes when I tried to bind a check box inside a DataGrid to a public property of a the View Model which is the data context of the user control.
thanks,
Jdang
<Custom:DataGrid ItemsSource="{Binding Customers}"
AlternatingRowBackground="AliceBlue"
AutoGenerateColumns="False"
MaxHeight="250"
CanUserAddRows="False"
CanUserDeleteRows="False" >
<Custom:DataGrid.Columns>
<Custom:DataGridTemplateColumn>
<Custom:DataGridTemplateColumn.Header>
<WrapPanel>
<CheckBox IsChecked="{Binding Path=IsCheckAll, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},
UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock>Select<LineBreak/>UnSelect</TextBlock>
</WrapPanel>
</Custom:DataGridTemplateColumn.Header>
<Custom:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Selected, Mode=TwoWay}"/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellTemplate>
</Custom:DataGridTemplateColumn>
The usercontrol you try to find, is (I assume as it's not in the code snippet) in the logical tree.
The binding is in the template which means that it's part of the visual tree. As they don't have a connection you cannot find it.

Can't get to the properties of the datacontext

I have a Grid in a User Control that's placed in a Window that has 2 collections.
I'm looking for a way to get to Collection 2 from within my grid.
I already tried a couple of things:
ItemsSource="{Binding DataContext.Bicycles, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type CollectionContainer}}}" />
and
<ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding RelativeSource=
{RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1},
Path=DataContext.Bicycles}" DisplayMemberPath="Height" />
and
<ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding RelativeSource=
{RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Companies}"
/>
But everytime my combobox ends up empty
When you run your app you should look in your 'Output' window that will tell you the binding errors that come up. So it will give you a clue as to what you're doing wrong.
It looks like you don't need the prefix DataContext. the datacontext of a child control is the datacontext of it's parent by default unless otherwise specified. So if the DataContext of the Window is some ViewModel the UserControl and it's child controls will have the same datacontext.
So you should probably only have to do this:
<ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding Companies}" />

Silverlight 3.0 and MVVM Pattern

i am working on this article here they are using Silverlight 4.
link text
but i am using Silverlight 3.but for button we are not able to find the command
Command="{Binding Path=DataContext.GetPerson, ElementName= LayoutRoot }"
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="53,112,0,0" Name="button1"
VerticalAlignment="Top" Width="75" Command="{Binding Path=DataContext.GetPerson, ElementName= LayoutRoot }"
CommandParameter="{Binding Path=Age, ElementName=hi}" />
so in Silverlight 3 what should i do to get this Command property for the button
any help would be great
A command is a bindable way of associating an action to a control (button for instance).
The command controls 2 things:
whether it can be executed
what to do when it's executed
by implementing the ICommand interface
They've been around for a while in WPF and were recently added to SL4
If you want your code to work in SL3 you'll have to bind the IsEnabled property to a property in your view model that has the same logic as the CanExecute implementation of the command
and to add a Click event handler for the button which has the same logic has the command's Execute
Also, this button passes a parameter to the command (CommandParameter), you'll have to handle passing this parameter manually
You could use a framework like prism, add the relevant dll's to your project, then add a reference to the top of page like so
xmlns:prism="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
Then declare the prison attribute on your button
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="53,112,0,0" Name="button1"
VerticalAlignment="Top" Width="75" Command="{Binding Path=DataContext.GetPerson, ElementName= LayoutRoot }"
prism:Click.Command={Binding SomeCommand}
prism:Click.CommandParameter="{Binding Path=Age, ElementName=hi}" />

Binding AutoCompleteBox inside DataTemplate

I have the following AutoCompleteBox defined inside DataTemplate:
<Window.Resources>
<DataTemplate x:key="PaneTitleTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinition>
<ContentPresenter Content="{Binding}" />
<toolkit:AutoCompleteBox x:Name="InsertBox" ItemsSource="{???}" />
</Grid>
</DataTemplate>
</Window.Resources>
...
<radRock:RadPane x:Name="pane1" TitleTemplate="{StaticResource PaneTitleTemplate}"/>
Now I'd like to fill it with a list of strings, but I don't know which Binding should I use. The list of strings is an instance variable from the Window. What should I do?
Part of the question is what is your DataContext. If it is the Window itself or is it some other object. If it is the Window then you don't need to specify it in the binding, if it some other object then you must specify that you are using the Window as the binding source. I think the binging you want is as follows (you can remove the ElementName if the Window is the DataContext):
ItemsSource="{Binding StringListName, ElementName=WindowName}"
Obviously replace StringListName and WindowName with the name they actually have in your window.

Placing common contols in two tab pages

I have a tab item with 2 tab pages. I want to place 4 check boxes in each of the tab pages with all check boxes having the same property. How it can be done easily?
Bind all your checkboxes to the same property (from your ViewModel, you do have one, right?). Then they will all display the same information.
There's not much to the binding code.. You could try something like...
<TabControl>
<TabItem Header="Tab1">
<StackPanel>
<CheckBox Content="One" Checked="{Binding Path=MyProperty}"/>
<CheckBox Content="Two" Checked="{Binding Path=MyProperty}"/>
</StackPanel>
</TabItem>
<TabItem Header="Tab2">
<StackPanel>
<CheckBox Content="Three" Checked="{Binding Path=MyProperty}"/>
<CheckBox Content="Four" Checked="{Binding Path=MyProperty}"/>
</StackPanel>
</TabItem>
</TabControl>
..where MyProperty is a local property representing the state you want to show.

Resources