Binding AutoCompleteBox inside DataTemplate - binding

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.

Related

Windows Phone Binding

In my RoomView.xaml I have:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox ItemsSource="{Binding myStrings, Mode=TwoWay}"></ListBox>
</Grid>
In my constructor I am doing:
var myStrings = new List<string>{"Usmaan","Carl","Andy","Saul"};
DataContext = myStrings;
Yet nothing is being spat out on the page when I load the application.
Can anyone see where I am going horribly wrong?
The DataContext of your page is already set to the List object, so you just need to set the binding like this:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox ItemsSource="{Binding, Mode=TwoWay}"></ListBox>
</Grid>
Alternatively, you could create an object that has a MyStrings property and use it as the DataContext of the page. Then you could bind the ListBox like you did {Binding myStrings, Mode=TwoWay} while also being able to bind other controls to other properties of that object (that's the principle of ViewModels).

How to make two independent instance of a ControlTemplate In a window?

i have a controltemplate with a textbox and a button,the button open a sub form to select something and show selected item in the textbox,like this:
<Window.Resources>
<ControlTemplate x:Key="CreateParam">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Button Content="select" Command="{Binding ShowSpecItemViewommand}" Grid.Column="0" Margin="2"/>
<TextBox Margin="2" Text="{Binding Param}" Grid.Row="0" Grid.Column="1"/>
<TextBlock Margin="5" Text="patameter" Grid.Row="0" Grid.Column="2"/>
</Grid>
</ControlTemplate>
</Window.Resources>
and i have a property in the viewmodel like this:
public string param;
public string Param
{
get
{
return param;
}
set
{
param = value;
RaisePropertyChanged("Param");
}
}
and now i want to create two independent instance of that control in a window,but when i select a value for the first instance,both of them have been changed.should i define two property?and how can i bind them to the control template?
i'm not sure that every one can understand what i mean,so i hope someone edit my question:)
How do you use the Control Template? To which Control do you attach this template? Is it a template for a custom control you have? Is it a template to a known control?
How do you instantiate the DataContext for the Control Template?
While you can implement what you want using ControlTemplate (and a custom control), and if you have many (i.e. much more than two, and all over) instances of your object a ControlTemplate may be the right paradigm, you'll be way better off using DataTemplate, or UserControl. There is more than one way to achieve what you want, but the code below is considered the "canonical" solution:
Say Param is a property of MyVM object. Then your XAML file should be:
<Window
x:Class="SO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:so="clr-namespace:SO"
Height="200" Width="350"
Title="SO Sample"
>
<Window.Resources>
<DataTemplate DataType="{x:Type so:MyVM}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Button Content="select" Command="{Binding ShowSpecItemViewommand}" Grid.Column="0" Margin="2"/>
<TextBox Margin="2" Text="{Binding Param}" Grid.Row="0" Grid.Column="1"/>
<TextBlock Margin="5" Text="patameter" Grid.Row="0" Grid.Column="2"/>
</Grid>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ContentControl>
<so:MyVM Param="1234" />
</ContentControl>
<ContentControl>
<so:MyVM Param="5678" />
</ContentControl>
</StackPanel>
</Window>

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}" />

WPF Binding Button.Command in ControlTemplate to property of ViewModel

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.

Binding DataGridComboBoxColumn ItemsSource to RelativeSource FindAncestor doesn't work

I'm trying to use WPFToolkit's DataGrid control (and C#/.Net 3.5) to display a ComboBox per record. With the below code, the ComboBoxes show up but their drop-downs contain no items:
<wpftkit:DataGrid ItemsSource="{Binding TransactionToEdit.SisterTransactions}"
AutoGenerateColumns="False">
<wpftkit:DataGrid.Columns>
<wpftkit:DataGridComboBoxColumn Header="Account" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}, diagnostics:PresentationTraceSources.TraceLevel=High}, Path=DataContext.Accounts}" DisplayMemberPath="Name"/>
</wpftkit:DataGrid.Columns>
</wpftkit:DataGrid>
Additionally, Visual Studio's output window shows the following error:
System.Windows.Data Error: 4 : Cannot find source for binding with
reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.StackPanel', AncestorLevel='1''.
BindingExpression:Path=DataContext.Accounts; DataItem=null; target element is
'DataGridComboBoxColumn' (HashCode=25733404); target property is
'ItemsSource' (type 'IEnumerable')
However, the following code works as expected (the ComboBoxes' drop down lists are correctly populated):
<ItemsControl ItemsSource="{Binding TransactionToEdit.SisterTransactions}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=DataContext.Accounts, diagnostics:PresentationTraceSources.TraceLevel=High}" DisplayMemberPath="Name"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Note that both the DataGrid and the ItemsControl have identical ItemsSource strings. So do the DataGridComboBoxColumn and the ComboBox. One control binds correctly and the other does not.
Why doesn't the DataGridComboBoxColumn ItemsSource bind properly?
Thank you,
Ben
FYI, diagnostics is defined as xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Interesting...if I create a custom DataGridColumn containing a ComboBox and use the same ItemsSource binding string as given above, it works.
<wpftkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Account}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=DataContext.Accounts}" DisplayMemberPath="Name" />
</DataTemplate>
</wpftkit:DataGridTemplateColumn.CellTemplate>

Resources