How bind data from multiple datasource field in a single gridcontrol cell - wpf

I'm a beginner with WPF. I need to show two textblock in a single gridcontrol cell and bind them to a different fields from data source. The first step is clear for me. But I can't understand how I should bind Text property of TextBlock to datasource properies.
Please, give me some notes and advices.
Below some code (xaml) lines. So I need to show in the first TextBlock property ProjectPriority and in the second - ProjectDeadLine.
That's my datasource
<dx:LinqCollectionViewDataSource x:Key="LinqCollectionViewDataSource" Culture="ru" ContextType="{x:Type ORM:FPMDataContext}" CollectionViewType="{x:Type ListCollectionView}" dx:DesignDataManager.DesignData="{dx:DesignDataSettings RowCount=5, UseDistinctValues=True}" Path="ActualProjectView">
<dx:LinqCollectionViewDataSource.SortDescriptions>
<ComponentModel:SortDescription Direction="Ascending" PropertyName="ProjectDeadlineDate"/>
<ComponentModel:SortDescription Direction="Descending" PropertyName="ProjectPriority"/>
<ComponentModel:SortDescription Direction="Descending" PropertyName="ProjectCreationDate"/>
</dx:LinqCollectionViewDataSource.SortDescriptions>
</dx:LinqCollectionViewDataSource>
And that's GridControl:
<dxg:GridControl AutoPopulateColumns="True" ItemsSource="{Binding Data, Source={StaticResource LinqCollectionViewDataSource}}" Margin="0,-10,0,10">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="ProjectPriority" >
<dxg:GridColumn.DisplayTemplate>
<ControlTemplate>
<StackPanel>
<TextBlock Text="{Binding ProjectPriority}"/>
<TextBlock Text="{Binding ProjectDeadLine}"/>
</StackPanel>
</ControlTemplate>
</dxg:GridColumn.DisplayTemplate>
</dxg:GridColumn>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView ShowTotalSummary="True" ShowAutoFilterRow="True" FocusedViewChanged="TableView_FocusedViewChanged"/>
</dxg:GridControl.View>
</dxg:GridControl>

With a ItemsControl like a GridControl your Itemssource is a Collection of Type T. Inside your Column you can access the Properties of class T via Binding.
It seems you have a Collection called Data. The Framework will look for the ProjectPriority Property in each Entry of this List.

Related

Bind the Text Property of Textblock to a Combobox selectedItem with different DataContext in Silverlight

I have a Data Grid that is bound to a object of type MyStaff. Apart from other properties MyStaff contains a column named LookupID. Now, in my ViewModel I have a collection Named Lookups that have a description for each LookupID.
I have a Template column that has a Textblock in Cell Template and Combobox in CellEdit Template. How do I bind the Textblock so that it dsiplays the description from ComboBox based on LookupID.
I know it would be pretty simple if the datacontext for both the Textblock and ComboBox were simple but that is not the case.
I have tried this but this doesn't work. Any suggestions? Also would appreciate any information on how to best use different Data Context for different controls in Silverlight. For this I have added a static resource pointing to the ViewModel Class.
<sdk:DataGridTemplateColumn Header="Action Point"
Width="500"
CanUserReorder="False"
HeaderStyle="{StaticResource dthFull2}"
IsReadOnly="False">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding ElementName=LookupList,
Path=SelectedItem.Description}"
MinHeight="24"
VerticalAlignment="Top"
Padding="2"
TextTrimming="WordEllipsis"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<my:AutoCompleteComboBox x:Name="LookupList"
FilterMode="Custom" Margin="2,0,0,0"
SelectedValue="{Binding LookupID, Mode=TwoWay}"
SelectedValuePath="LookupID"
ItemsSource="{Binding Path=AnalysisLookupList.Values,
Source={StaticResource ViewModel}}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>

Devexpress ComboBoxEditSettings not binding to ViewModel in WPF

I'm having a problem with the DevExpress wpf grid binding when using the ComboBoxEditSettings. Below is the grid. I'm binding the "Associated Well Types" column to a property on my ViewModel. However, when I select some items from the combo box and then click "Ok", I see the selected items in the cell, but they don't bind back to my ViewModel. So, do I need to do something with the "Ok" button's click event? Is there
something in the binding that I'm not setting up properly? All of the above?
Thanks in advance for your help.
<ctrls:GridControl Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="5"
dx:ThemeManager.ThemeName="Office2007Blue"
x:Name="gridControlForms" VerticalAlignment="Stretch"
ShowLoadingPanel="{Binding TaxFormsIsBusy}"
ItemsSource="{Binding TaxFormList}" >
<ctrls:GridControl.Columns>
<dxg:GridColumn FieldName="DisplayName" Header="Form Name" VisibleIndex="1" AllowMoving="False" />
<dxg:GridColumn ReadOnly="True" FieldName="PrintSequence" Header="Print Sequence" VisibleIndex="2" AllowMoving="False" />
<dxg:GridColumn Header="Associated Well Types" FieldName="SelectedWellTypes" VisibleIndex="3" AllowMoving="False" AllowBestFit="True" Width="300" >
<dxg:GridColumn.EditSettings>
<dxe:ComboBoxEditSettings ItemsSource="{Binding WellTypes}" DisplayMember="WellType" ValueMember="RenditionStateFormWellTypeId" IsTextEditable="False" >
<dxe:ComboBoxEditSettings.StyleSettings>
<dxe:CheckedComboBoxStyleSettings />
</dxe:ComboBoxEditSettings.StyleSettings>
</dxe:ComboBoxEditSettings>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
</ctrls:GridControl.Columns>
<ctrls:GridControl.View>
<dxg:TableView ShowGroupPanel="False" BestFitArea="All" AllowSorting="False"
FocusedRow="{Binding SelectedTaxForm, Mode=TwoWay}" AllowColumnFiltering="False" />
</ctrls:GridControl.View>
I discovered the problem: the grid was throwing an "Object must implement IConvertible" error. Once I discovered that, I changed SelectedWellTypes from an int to a IEnumberable of type object and did the appropriate conversions in the viewmodel. This allowed the binding to occur.

How to close the DevExpress WPF ComboBox when the Row of a Nested Grid is selected?

I am using DevExpress WPF controls, expessicaly the BarEditItem, ComboBoxEdit and GridControl.
Normally when you use the standard ComboBoxEdit, when you select an item, the combobox closed.
However, I am now setting the ComboBoxEditSettings.PopupContentTemplate to a GridControl.
Whenever I select a row in the GridControl (by clicking on it), I want the dropdown to close.
It works when I hit the key, but not when I simply click a record.
The XAML looks like this:
<dxb:BarEditItem.EditSettings >
<dxe:ComboBoxEditSettings
ItemsSource="{Binding Path=EditieCollection}"
DisplayMember="JaarEditieNummer"
AutoComplete="True"
Name="editieComboBox" AcceptsReturn="True" IncrementalFiltering="False" ImmediatePopup="True">
<dxe:ComboBoxEditSettings.PopupContentTemplate >
<ControlTemplate>
<dxg:GridControl
Name="PART_GridControl"
ItemsSource="{Binding Path=EditieCollection}"
AutoPopulateColumns="False" ShowBorder="False"
>
<dxg:GridControl.Columns>
<dxg:GridColumn x:Name="EditieJaarEditieNummer" FieldName="JaarEditieNummer" Header="Editie" Width="90" />
<dxg:GridColumn x:Name="EditieOplage" FieldName="OplageDatum" Header="Oplage" Width="90" />
<dxg:GridColumn x:Name="EditieVerschijning" FieldName="Datum" Header="Verschijnt" Width="90" />
<dxg:GridColumn x:Name="EditieOmschrijving" FieldName="Omschrijving" Header="Omschrijving" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView
Width="Auto"
AllowGrouping="False" IsGroupPanelMenuEnabled="False"
FocusedRow="{Binding Path=SelectedEditie, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AllowEditing="False" AllowMoveColumnToDropArea="False" AllowMoving="False"
NavigationStyle="Row" ShowGroupPanel="False"
/>
</dxg:GridControl.View>
</dxg:GridControl>
</ControlTemplate>
</dxe:ComboBoxEditSettings.PopupContentTemplate>
</dxe:ComboBoxEditSettings>
</dxb:BarEditItem.EditSettings>
I have seen other solutions where one would set the .IsOpen property of the standard ComboBox control to false, but DevExpress ComboBoxEdit doesn't have that sort of property (at least not that I know of).
Thanks!
I am now setting the ComboBoxEditSettings.PopupContentTemplate to a
GridControl.
I believe the LookUpEdit instead of combobox is better choice for you in this case:
<dxb:BarEditItem Name="bEditItem" >
<dxb:BarEditItem.EditSettings >
<dxg:LookUpEditSettings ItemsSource="{Binding ...}"
DisplayMember="..."
ValueMember="...">
</dxg:LookUpEditSettings>
</dxb:BarEditItem.EditSettings>
</dxb:BarEditItem>
Anyway, you can use the IsPopupOpen property to close popup window.

Problem binding DataGridComboBoxColumn.ItemsSource

I have 3 tables:
Item - which is the DataContext - it has a navigation column Group
Group - has a navigation column Category.
I want to have in the DataGrid both (Category & Group) columns and when I choose a category it should display in the group col only the Category.Groups.
Here is the code I am working on:
<tk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}">
<tk:DataGrid.Columns>
<!--Works-->
<tk:DataGridComboBoxColumn
Header="Categroy"
DisplayMemberPath="Title"
SelectedValuePath="CategoryId"
SelectedValueBinding="{Binding Group.Category.CategoryId}"
ItemsSource="{Binding Context.Categories,
Source={x:Static Application.Current}}"
/>
<!--Look at these two things:-->
<!--This does work-->
<tk:DataGridTemplateColumn>
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl
ItemsSource="{Binding Group.Category.Groups}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type data:Group}">
<TextBlock Text="{Binding Title}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
<!--But this does NOT work, even it's the same source-->
<!--Notice I even tried a dummy converter and doesnt reach there-->
<tk:DataGridComboBoxColumn
Header="Group"
DisplayMemberPath="Title"
SelectedValuePath="GroupId"
ItemsSource="{Binding Group.Category.Groups,
Converter={StaticResource DummyConverter}}"
SelectedValueBinding="{Binding Group.GroupId}"
/>
</tk:DataGrid.Columns>
</tk:DataGrid>
Update
Would you say the problem is that the ItemsSource property cannot be set to a non-static Binding?
I suspect so because even I set the ItemsSource to {Binding} with the DummyConverter it doesn't stop in the converter; and in the Category ComboBox it works fine.
The columns in the datagrid don't have a datacontext, as they are never added to the visual tree. sound a bit weird but have a look at vince's blog, its got a good example of the visual layout. once the grid is drawn the cells have a data context and you can set the combo boxes items source in them using normal bindings (not static resources..)
You can access the combo box items source as such:
<dg:DataGridComboBoxColumn>
<dg:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=MyBindingPath}" />
</Style>
</dg:DataGridComboBoxColumn.EditingElementStyle>
</dg:DataGridComboBoxColumn>
Have a look here and also here for some code. You will also need to set the items source for the non edting element as in this post
I was using MVVM and I wanted to bind the ItemSource of the column to a collection of objects in the window data context. I must have tried 10 different ways and nothing worked until I found this answer.
The trick is to define a CollectionViewSource outside the grid and then reference it inside the grid using StaticResource. For example,
<Window.Resources>
<CollectionViewSource x:Key="ItemsCVS" Source="{Binding MyItems}" />
</Window.Resources>
<!-- ... -->
<DataGrid ItemsSource="{Binding MyRecords}">
<DataGridComboBoxColumn Header="Column With Predefined Values"
ItemsSource="{Binding Source={StaticResource ItemsCVS}}"
SelectedValueBinding="{Binding MyItemId}"
SelectedValuePath="Id"
DisplayMemberPath="StatusCode" />
</DataGrid>

How can I achieve a binding path which is bound by another binding?

I'm writing my own UserControl which displays data in a ListBox. I want to achieve something similar to a property like "DisplayMemberPath". In my example, it's "EditableMemberPath", which should determine which member will be displayed in the textbox. However, using a binding on a binding does not work.
<ListBox ItemsSource="{Binding Path=Items, ElementName=me}"
SelectedItem="{Binding Path=SelectedItem, ElementName=me}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Converter={Binding Path=ItemToImageConverter, ElementName=me}}" />
<TextBlock Text="{Binding Path={Binding Path=EditableMemberPath, ElementName=me}}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Can you help me finding a proper solution?
Best Regards
Oliver Hanappi
You can't do that directly in XAML (at least not with the built-in classes) : a Binding is not a DependencyObject, so you can't bind its properties.
However the post mentioned by Martin Harris looks promising...

Resources