Devexpress ComboBoxEditSettings not binding to ViewModel in WPF - 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.

Related

WPF Clearing Validation in BindingGroup

I have a class called Contact that has 2 string properties: "Name" and "PhoneNumber".
My ViewModel has an observable collection of these contacts and there is a ComboBox that has it's ItemsSource bound to this collection .
I have a WPF Grid whose DataContext is set to the ComboBox's SelectedItem property. This Grid has a BindingGroup that contains a ValidationRule to validate the text properties of my "contact" class.
This is my Grid:
<Grid x:Name="ContainerGrid" Grid.Column="0"
Background="Transparent"
LostFocus="ContainerGrid_LostFocus"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}">
<Grid.BindingGroup>
<BindingGroup x:Name="TextInputBindingGroup" SharesProposedValues="True">
<BindingGroup.ValidationRules>
<local:TextInputValidationRule />
</BindingGroup.ValidationRules>
</BindingGroup>
</Grid.BindingGroup>
<StackPanel>
<TextBlock Text="Name:" />
<TextBox x:Name="PersonName" Text="{Binding Path=PersonName, ValidatesOnExceptions=True}" />
</StackPanel>
<StackPanel>
<TextBlock Text="Phone Number:" />
<TextBox x:Name="PhoneNumber" Text="{Binding Path=PhoneNumber, ValidatesOnExceptions=True}" />
</StackPanel>
</Grid>
If the data for the contact was invalid (the name and/or phone number was missing) the grid containing the elements for editing these properties is highlighted in red and it's tool tip is set to the first error.
If I then delete the invalid Contact, the grid remains outlined in red with the tool tip displaying the error for the item that was deleted even though the grid is now displaying another, valid item.
How do I clear the errors shown in the grid?
Thanks,
-Frinny

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

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.

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.

Pattern for working with a form in Silverlight 4? (How to get references to XAML elements)

I have a form:
<StackPanel Orientation="Horizontal" Visibility="{Binding Editable, Converter={StaticResource visibilityConverter}}"
ToolTipService.ToolTip="Add new topic to this group">
<sdk:AutoCompleteBox Width="160" ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.TopicNames}" />
<Button Click="addTopicButton_Click">
<Image Source="Images/appbar.add.rest.png" />
</Button>
</StackPanel>
This form appears in a DataTemplate for an ItemsControl. I'm not sure what the best way is to get the data from the AutoCompleteBox when the button is clicked. I can't give the elements x:Name attributes, because they're in a template (right?).
How can I get around this? The Click event will give me the Button, but I need a reference to the text box. Use the Button's parent, then look through the children for the Textbox? If I factored this out into its own UserControl, I could set x:Name values, but I'd rather not do that.
Any other ideas?
Update: Here is another example of such a problem:
<ListBox x:Name="topicList"
ItemsSource="{Binding Id, Converter={StaticResource topicGroupIDConverter}}"
SelectionChanged="ListBox_SelectionChanged"
HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
Width="150"
VerticalAlignment="Center"
ToolTipService.ToolTip="{Binding Description}"
ToolTipService.Placement="Right" />
<Button ToolTipService.ToolTip="Remove this topic from this group"
Visibility="{Binding ElementName=topicList,
Path=DataContext.Editable,
Converter={StaticResource visibilityConverter}}"
Click="removeTopicButton_Click"
HorizontalAlignment="Right"
Margin="10,0">
<Image Source="Images/appbar.cancel.rest.png" />
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When the button is clicked, I want to access topicList.DataContext. However, topicList itself is a DataTemplate in an ItemsControl, so I can't access it using its name from code-behind. How else can I do this?
You can add a property, say SelectedItemInAutoCompleteBox, to your presenter, and then can bind it to the SelectedItem property of AutoCompleteBox, using Mode=TwoWay, like this,
<sdk:AutoCompleteBox SelectedItem="{Binding Path=DataContext.SelectedItemInAutoCompleteBox, Mode=TwoWay}" ... />
You may try the same approach with Text property of AutoCompleteBox, also. See if it solves your problem.:-)
You have several choices:
If you're on Silverlight 5, use the AncestorBinding
Otherwise, use a Silverlight 4 AncestorBinding hack (it doesn't look pretty)
Or you could try DataContextProxy, which stores the DataContext in a resource so that it is accessible. Note: you should set the DataContextProxy as a Resource of topicList ListBox, not the UserControl as in Dan Wahlin's example.

Telerik Silverlight RadGridView - need to click buttons twice

I have a Silverlight UserControl with a Telerik RadGridView control and a handful of button controls. The problem is that when I edit a cell in the grid view and then click any of the buttons the button command does not execute until; the button is selected a second time. The first press seems to remove the focus from the RadGridView and second succeeds in firing the bound command. Obviously, this makes for a terrible user interface. Does anyone know what might be going on here?
With the exception of the UI behavior everything else works fine.
Simplified XAML:
<StackPanel Orientation="Horizontal">
<Button Content="Save" Command="{Binding SaveCommand}" />
<Button Content="Close" Command="{Binding CloseCommand}" />
</StackPanel>
<telerik:RadGridView ItemsSource="{Binding StockItems,Mode=TwoWay}"
AutoGenerateColumns="False"
IsFilteringAllowed="False"
ShowGroupPanel="False">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding LabelGroup,Mode=TwoWay}"
Header="Label Group" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding Message,Mode=TwoWay}"
Header="Message" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
Thanks in advance,
Jim
The problem is already fixed - you can check this thread for more info:
http://www.telerik.com/community/forums/silverlight/gridview/why-do-we-need-to-click-twice-on-a-button-within-a-radgridview.aspx

Resources