Bind to selected rows in GridControl - wpf

I would like to get a list of the objects selected in GridControl (DevExpress)
Would you know how I could do so?
<dxg:GridControl ItemsSource="{Binding Cars}">

Found the way to do this.
Way 1
If having DevExpress 15.5 (which I don't), simply use the SelectedItem property: https://documentation.devexpress.com/#WPF/DevExpressXpfGridDataControlBase_SelectedItemstopic
Way 2
Create a behavior to hook the TableView inside the GridControl if you have:
<dxg:GridControl ItemsSource="{Binding Cars}">
<dxg:GridControl.View>
<Controls:TableView >
<i:Interaction.Behaviors>
<view:CollectionObserverBehavior SelectedItems="{Binding SelectedCars}" />
</i:Interaction.Behaviors>
</Controls:TableView>
Behavior details in the good article here: http://social.technet.microsoft.com/wiki/contents/articles/22871.wpfmvvm-binding-to-read-only-properties-using-behaviors.aspx

Related

How to use a RibbonGallery with a RibbonComboBox

So I wanted to add a RibbonCombobox to my Ribbon in WPF. For some reason, RibbonCombobox does not have a selectionchanged event. I read that you should use a RibbonGallery for selection change event so I implemented this
<RibbonComboBox Label="Equations" x:Name="EquationListComboToolbar" ItemsSource="{Binding}">
<RibbonGallery x:Name="EquationListComboboxGallery" SelectedValue="{Binding XPath=.}" />
</RibbonComboBox>
Behind the scene the binding is done like this.
EquationListComboToolbar.DataContext = ViewModel.EquationNames;
this.Bind(ViewModel, vm => vm.SelectedEquation, v => v.EquationListComboboxGallery.SelectedItem).DisposeWith(cleanup);
Observable.FromEventPattern(EquationListComboboxGallery, nameof(EquationListComboboxGallery.SelectionChanged)).Subscribe(e => ViewModel.SelectEquation(EquationListComboboxGallery.SelectedItem?.ToString()));
At runtime I get the following error
"An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
Items collection must be empty before using ItemsSource." When the app initalizez. I know it's something about the Gallery but I can't figure out what is the problem and how can I achieve this.
As I was suggested, I already tried the answer that was suggested
<RibbonComboBox Label="Equations" x:Name="EquationListComboToolbar" ItemsSource="{Binding}">
<RibbonComboBox.ItemTemplate>
<DataTemplate>
<RibbonGallery x:Name="EquationListComboboxGallery" SelectedValue="{Binding XPath=.}" />
</DataTemplate>
</RibbonComboBox.ItemTemplate>
</RibbonComboBox>
Doing this, will make by binding imposible
Ah, yes. The Microsoft ribbon library is lots of fun. Luckily I've been down this road before. Here's a working example of a RibbonComboBox from one of my applications, complete with RibbonGallery:
<RibbonComboBox DropDownHeight="400">
<RibbonGallery MaxColumnCount="1" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedItem="{Binding MySelectedItemProperty}">
<RibbonGalleryCategory ItemsSource="{Binding MyItemsSourceProperty}"/>
</RibbonGallery>
</RibbonComboBox>
I'm not entirely sure this is the only way to do things, but I know this way works. Note that I set ItemsSource on the RibbonGalleryCategory, not the RibbonComboBox itself. It might be possible to use the RibbonGallery without a RibbonGalleryCategory, in which case you would set ItemsSource on RibbonGallery, but I've not tested this.
Note you also have the ability to add multiple galleries categories to a single RibbonComboBox like so:
<RibbonComboBox DropDownHeight="400">
<RibbonGallery MaxColumnCount="1" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedItem="{Binding MySelectedItemProperty}">
<RibbonGalleryCategory ItemsSource="{Binding MyFirstItemsSourceProperty}"/>
<Separator/>
<RibbonGalleryCategory ItemsSource="{Binding MySecondItemsSourceProperty}"/>
</RibbonGallery>
</RibbonComboBox>
The above lets you show multiple lists in the same drop down and allows the user to select a single item from any list. Functionality like this is probably why RibbonGalleryCategory exists in the first place.

Why ItemTemplate will make items disappear for ListView?

I was solving Windows Phone 8.1 ListView wobbling problem and had code like below, however, once I add the ItemTemplate, the contents of the List cannot be seen, I'm wondering why and how to fix the problem.
<ListView
Grid.Row="1"
x:Name="ListViewEvents"
Loaded="OnListViewEventsLoaded"
ItemsSource="{Binding xx}"
ItemTemplateSelector="{StaticResource xx}"
ItemContainerStyle="{StaticResource xx}"
IsItemClickEnabled="True">
<ListView.ItemTemplate >
<DataTemplate >
<Grid Width="{Binding ActualWidth, ElementName=EventsListGrid}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Your Width binding is trying to read the ActualWidth property of an element in your XAML named EventsListGrid, but there's no such element in the sample code you've provided. As such, the binding engine is unable to set the Width property on your grid, most likely setting it to some unset/NaN value. At least I can confirm this when setting up a similar case as the one you provided and inspecting in Snoop the ListViewItem containers generated for each item in the test collection bound to the ListView. Perhaps you want to set the element name to ListViewEvents in this case or some other parent element not shown in the example?

Columns Visibility for xceed DataGridControl

I have an xceed:DataGridControl with bounded ItemsSource. Currently I'm trying to set my in/visible columns and the title/headertext for each visible column. Preferably I would like to bind a property in my ViewModel, to set the in/visible columns and theirs titles. But I find no way I could do it. Does anyone know a solution to this problem?
<xceed:DataGridControl
x:Name="dataGridControl"
SelectedItem="{Binding SelectedTextItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ItemsSource="{Binding ItemsSourceData, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
</xceed:DataGridControl>
Yes indeed I had to deal with xceed's controls few months ago.
DataGridControl allows you to generate columns automantically. That is also its default behavior.
In order to have your own columns you will have to disable the property AutoCreateColumns and futhermore you will have to set few columns on the property DataGridControl.Columns.
There you will be able to bind Visible property of the Column.
Thanks to Peter for providing this code:
<xceed:DataGridControl ItemsSource="{Binding TextSet}" >
<xceed:DataGridControl.Columns>
<xceed:Column FieldName="ColumnId" Title="{Binding DatagridTitle[ColumnId], Mode=OneWay}" Visible="True" />
</xceed:DataGridControl.Columns>
</xceed:DataGridControl>
I also met similar issue.
You can use the Visible property, then do following:
<xcdg:ColumnFieldName="ColumnId" Title="ColumnId"
Visible="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type typeOfAncestor}}, Path=DataGridControl.DataContext.BooleanSourceProperty}"/>
For example, if the typeOfAncestor is xcdg:MergedColumn and BooleanSourceProperty is IsVisble, then the code should be:
<xcdg:ColumnFieldName="ColumnId" Title="ColumnId"
Visible="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type xcdg:MergedColumn}}, Path=DataGridControl.DataContext.IsVisible}"/>
Then the issue can be solved.

DataBinding to a List in silverlight

I've got a Grid which contains a TextBlock. The Grid's DataContext is of type List<MyClass>, and I'd like to bind the TextBlock.Text property to the MyClass.MyProperty property of first element in the List. I tried something like:
<Grid x:Name="RootLayout">
<TextBlock Text="{Binding [0].MyProperty}" />
</Grid>
But of course, that did not work. What's the right way of doing this?
Edit:
I'm going to try and make my explanation more clear. I've got multiple elements in the grid, each of which binds to a different item in the list. The items are laid out in a customized manner which cannot be accomplished by a GridView or ListBox. One of the items in the Grid is the TextBlock, and I'd like to bind its Text property to a property of the first element in the list. Once I know how to do that, I can extend that knowledge to add bindings to the rest of the elements in the grid.
Edit 2:
Turns out, my code works just fine in Silverlight. My project is actually a WinRT project, but I figured I'd get quicker answers if I tagged it as Silverlight, since databinding is supposed to work the same. I'm assuming this is a bug in WinRT, so I'll just have to find a workaround for it :(
I'm not sure I get why you want to do this, but you could create a property that returns what you want from the item in the list like so:
public string MyBindingProperty
{
get { return MyList != null && MyList.Count > 0 ? MyList[0].MyProperty : "Error Text"; }
}
Then you'd bind to MyBindingProperty:
<TextBlock Text="{Binding MyBindingProperty}" />
EDIT
I was wrong in saying you can't get at the items in the List - my bad. Your binding should look like this:
<TextBlock Text="{Binding [0].MyProperty}" />
If you need me I'll be in the corner enjoying my humble pie.
I am not an expert of SL but I think your are using the wrong Grid object; try with DataGrid in this way:
<data:DataGrid x:Name="targetDataGrid">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="MyProperty"
Binding="{Binding MyProperty}" />
</data:DataGrid.Columns>
</data:DataGrid>
also see here for more details: Defining Silverlight DataGrid Columns at Runtime
Edit: then go this way:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding MyProperty}" />
</Grid>
found here: http://msdn.microsoft.com/en-us/library/cc278072%28v=VS.95%29.aspx scroll donw the article...

Silverlight 3: Using list of UserControls as ItemsSource for TreeView

I want to populate a TreeView with UserControls, but I only want the Name property to show up, not the entire UserControl. The following code gives me weird crashes as soon as I add something to myUCs:
C#:
var myUCs = new ObservableCollection<UserControl>();
MyTreeView.ItemsSource = myUCs;
XAML:
<controls:TreeView x:Name="MyTreeView">
<controls:TreeView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>
Does anyone know how to use a list of UserControls as an ItemSource for TreeViews?
I found one not so convenient workaround: instead of a List of UserControls, use a Dictionary, and change the XAML to:
<controls:TreeView x:Name="MyTreeView">
<controls:TreeView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Key.Name}"/>
</DataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>
The same bug(?) exists in ListBox, a solution is provided here:
Use UIElements as ItemsSource of ListBox in Silverlight
That particular fix does not work for TreeView
You may have to create your own class that extends UserControl and override the ToString() method so that it returns the name property.

Resources