WPF XAML Binding - binding

I have 2 tables Main and Maintest. I am using nhibernate to pull data from database and am joining 2 tables to fetch the fields from both.Now my final object has data from both the tables. Now when I debug my app I can see that I have 2 records from Main and 5 records from Maintest. But somehow I am not able to display records from Maintest.
<DataTemplate x:Key="myTaskTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{Binding Main}" ItemTemplate="{StaticResource myTaskTemplate}" Height="200" Width="200" />
<toolkit:DataGrid ItemsSource="{Binding Main.Maintest}" Margin="3"
AutoGenerateColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserReorderColumns="False" CanUserResizeRows="False">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="#"
Binding="{Binding Number}"/>
<toolkit:DataGridTextColumn Header="Airline"
Binding="{Binding Code}"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</StackPanel>
NHibernate Mapping:
<class name="Main" lazy="false">
<id name="ID" type="Int32">
<generator class="native"/>
</id>
<set name="Maintest" inverse="true">
<key column="Ticket" on-delete="cascade" />
<one-to-many class="Segment" />
</set>
....
I able to display listbox record but not toolkit records. Although I can see that for each Main record my object does have 3 or more records in Maintest.

Something doesn't add up...
ListBox.ItemsSource takes an IEnumerable of some kind - which means the property Main must be some kind of IEnumerable?
So, if what you're looking for is a Master-Detail kind of view - you need to change the XAML for the two controls like this - everything else should be fine:
<ListBox Name="Main" .../>
<toolkit:DataGrid ItemsSource="{Binding SelectedItem.Maintest,ElementName=Main}" .../>
This will make the DataGrid bind to the MainTest property of whatever obejct is selected in the ListBox.
Look to Bea Costa if you really need to get spiffy with Master-Detail scenarios.
Hope this helps!

Related

How to highlight selected row in a listbox for windows phone

I am trying to highlight the selected row in a list box and I am having difficulty getting it to work. I have looked online and the solutions that I found either involve using style.resources or style.trigger, both of which my program is saying aren't recognized.
The xaml code for my listbox is as follows:
<ListBox x:Name="selectClassCanvas_ListBox" Width="448" Height="488"
SelectionChanged="selectListSelectionChanged" Grid.RowSpan="2"
Canvas.Left="4" Canvas.Top="54" Background="White">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Width="450" BorderBrush="Black" BorderThickness="0,0,0,1">
<TextBlock Text="{Binding}" FontWeight="Bold"
Foreground="Black" Height="50"
FontSize="17"/>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Can someone please give me some instruction on how to get the selected row of a listbox to highlight?

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

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