Unable to get SelectedItems through Xceed CheckListBox using MVVM - wpf

I have been using wpf xceed third party library for some of the UI components. I really like the way CheckListBox is being displayed at the screen. But I am not able to get the selectedItems bound to any property in the viewmodel (the setter never triggers). Here is the code -
I am using a dataprovider to get values from enum -
<UserControl.Resources>
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="DeviceClassDataProvider">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="Model:HANDeviceClass" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
And then the control has been declared something like this -
<ext:CheckListBox Focusable="False" SelectedMemberPath="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" SelectedItemsOverride="{Binding SelectedDeviceGroups, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.RowSpan="7" Grid.Column="4" Padding="5" BorderThickness="0.8" BorderBrush="Gray" ItemsSource="{Binding Source={StaticResource DeviceClassDataProvider}}"/>
How will i get the selected items in it's viewmodel?
Any quick help would be highly appreciated!
Thanks in advance

Should work provided that SelectedDeviceGroups is a public property that returns an ICollection<HANDeviceClass>:
public ICollection<HANDeviceClass> SelectedDeviceGroups { get; } = new ObservableCollection<HANDeviceClass>();
XAML:
<ext:CheckListBox ItemsSource="{Binding Source={StaticResource DeviceClassDataProvider}}"
SelectedItemsOverride="{Binding SelectedDeviceGroups}" />
<TextBlock Text="{Binding SelectedDeviceGroups.Count}" />
Items will be added to and removed from the source collection as you check and uncheck items respectively.

Related

How to set IsChecked = True for a Radio buttons that are generated from an Enum WPF

I have generated RadioButtons from an enumeration as follows.
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="GetRadioTypes">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="Enums:RadioTypes"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ItemsControl ItemsSource="{Binding Source={StaticResource GetRadioTypes}}" VerticalAlignment="Center">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton Margin="10" GroupName="MyRadios" Checked="RadioButton_Checked" Content="{Binding}" >
</RadioButton>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Now i am interested to Check the first RadioButton generated defaultly.
How can i do that??
If i have IsChecked = "True" for the RadioButtton in ItemTemplate it by default Checks everyone that generates which i dont want to happen.
Thanks in advance
If you change your itemscontrol to, say, a list box you'll have access to the SelectedItem property. Bind that to a property in your viewmodel,SelectedRadio (or whatever), and then set SelectedRadio to the first enum in your vewmodel's constructor.

List colors in ComboBox without transparent color

I wan't in my application ComboBox with list of colors, but I don't won't this list to contain Transparent color. Additionaly colors in list must be from Colors class.
I search in net and find something like that:
<ObjectDataProvider MethodName="GetType"
ObjectType="{x:Type System:Type}" x:Key="colorsTypeOdp">
<ObjectDataProvider.MethodParameters>
<System:String>System.Windows.Media.Colors, PresentationCore,
Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35</System:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"
MethodName="GetProperties" x:Key="colorPropertiesOdp">
</ObjectDataProvider>
<ComboBox Width="80" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="18" Margin="0,0,0,2">
<Border BorderThickness="1" CornerRadius="2"
BorderBrush="Black" Width="50" VerticalAlignment="Stretch"
Background="{Binding Name}"/>
<TextBlock Text="{Binding Name}" Margin="8,0,0,0"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Anyone know how to do this taks? Thanks for any help.
I suggest you create your own list of Color instances from the Colors class and exclude the Colors.Transparent color from this list. Then you can bind this list to your combo box's ItemsSource property.
To get the list of Color instances from the Colors class, you can use the following code snippet:
PropertyInfo[] properties = typeof(Colors).GetProperties();
foreach (PropertyInfo property in properties)
Color color = property.GetValue(null, null) as Color;

WPF : How to create separate Resources for each item in a bound ItemsControl

I want to achieve the following:
My ViewModel exposes a property named 'Categories' which is a collection of CategoryViewModel objects
Each CategoryViewModel object exposes a property called 'Items' which is a collection of strings*.
On my View, I want a TabControl with a TabItem for each object in the 'Categories' collection.
The Content of each TabItem should be a xceed DataGrid control displaying the contents of the selected tab's Items collection.
<TabControl ItemsSource="{Binding Categories}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding CategoryName}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<xcdg:DataGridControl
ItemsSource="{Binding Items}"
AutoCreateColumns="True">
</xcdg:DataGridControl>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
This works ok when I bind directly to the ItemsSource property of the DataGridControl. However, in order to utilize all of the functionality of the DataGridControl, I need to bind the ItemsSource property of the DataGridControl to a DataGridCollectionViewSource object that is bound to my Items collection. I do this when the grid ISN'T nested in another control by creating a DataGridCollectionViewSource object in the Resources section of the UserControl and binding to that.
<UserControl>
<UserControl.Resources>
<xcdg:DataGridCollectionViewSource x:Key="GridData"
Source="{Binding Items}" />
</UserControl.Resources>
<Grid>
<xcdg:DataGridControl
ItemsSource="{Binding Source={StaticResource GridData}}"
AutoCreateColumns="True">
</xcdg:DataGridControl>
</Grid>
</UserControl>
How do I need to structure the XAML so that when the TabControl is being bound, a DataGridCollectionViewSource object is created for each TabItem so that the DataGridControl that is generated within the content of the TabItem can be bound to it?
Clear as mud, right? :)
Thanks!
Notes:
*In the real solution the collection contains objects of a class that is more complex than a simple string, but a string was used to make the example more simple.
OK, this is a bit of a long-shot, but could you use the DataGrid.Tag ...
<TabControl.ContentTemplate>
<DataTemplate>
<xcdg:DataGridControl
ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"
AutoCreateColumns="True">
<xcdg:DataGridControl.Tag>
<xcdg:DataGridCollectionViewSource x:Key="GridData"
Source="{Binding Items}" />
</xcdg:DataGridControl.Tag>
</xcdg:DataGridControl>
</DataTemplate>
</TabControl.ContentTemplate>
Or ... resources can be defined on any FrameworkElement, so you could try:
<TabControl.ContentTemplate>
<DataTemplate>
<xcdg:DataGridControl
ItemsSource="{Binding Source={StaticResource GridData}}"
AutoCreateColumns="True">
<xcdg:DataGridControl.Resources>
<xcdg:DataGridCollectionViewSource x:Key="GridData"
Source="{Binding Items}" />
</xcdg:DataGridControl.Resources>
</xcdg:DataGridControl>
</DataTemplate>
</TabControl.ContentTemplate>
I don't use the eXceed Grid so cannot test whether these work - just a couple of ideas to try!
Colin E.
You can use x:Shared="True" attribute on a resource. That means a new instance is created for every use of that resource.
Example:
<UserControl.Resources>
<xcdg:DataGridCollectionViewSource x:Key="GridData"
x:Shared="False"
Source="{Binding Items}" />
</UserControl.Resources>

How can I bind a property of a user control to a properties of that same control in WPF?

Inside my user control I have a collection call Solutions
public List<string> Solutions { get; set; }
I want to bind that property to a combobox in xaml of that same user control?
I tried
<ComboBox HorizontalAlignment="Left" Margin="21,0,0,41" Name="cbAddSolution" Width="194" Height="21" VerticalAlignment="Bottom"
ItemsSource="{Binding Path=Solutions}" />
but that didn't seem to work.
Name your UserControl in XAML and refer to it from the binding like so:
<UserControl x:Name = "MyUserControl">
<ComboBox HorizontalAlignment="Left" Margin="21,0,0,41" Name="cbAddSolution" Width="194"
Height="21" VerticalAlignment="Bottom"
ItemsSource="{Binding ElementName=MyUserControl, Path=Solutions}" />
</UserControl>
If you want proper binding your UserControl should implement INotifyPropertyChanged for that property or make that property a Dependency Property
Update
Or use RelativeSource if you don't want to name the UserControl
<UserControl>
<ComboBox HorizontalAlignment="Left" Margin="21,0,0,41" Name="cbAddSolution" Width="194"
Height="21" VerticalAlignment="Bottom"
ItemsSource="{Binding Path=Solutions, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</UserControl>
Move the XAML of your control into the Template property, i.e. instead of
<UserControl x:Class="MyUserControl" ...>
...
<ComboBox ... />
...
</UserControl>
use
<UserControl x:Class="MyUserControl" ...>
<UserControl.Template>
<ControlTemplate>
...
<ComboBox ... />
...
</ControlTemplate>
</UserControl.Template>
</UserControl>
Then, you can use TemplateBinding:
<ComboBox ... ItemsSource="{TemplateBinding Solutions}" />
BTW, your question is very similar to this one: Custom UserControl Property used by child element

How to properly bind a ListBoxItem in WPF?

I have a listbox and I want to iterate over a collection of Bars in my Foo-object.
<ListBox DataContext="{Binding Path=Foo.Bars}" >
<ListBox.Items>
<ListBoxItem>
<ContentControl DataContext="{Binding Path=.}" />
</ListBoxItem>
</ListBox.Items>
</ListBox>
This is the datatemplate I want to use.
<DataTemplate DataType="{x:Type Bar}">
<Label Content="hello stackoverflow" />
</DataTemplate>
If I snoop (--> examine by using the tool Snoop) my application, I notice that the entire collection of Bars is bound to the ContentControl, in stead of just 1.
How can I properly bind so the iteration over the collection goes fine?
You can just set the DataTemplate, and WPF does all the work. Set the ItemsSource to a list of Bar items, and then define a DataTemplate for Bar items.
<ListBox ItemsSource="{Binding Path=Foo.Bars}">
<ListBox.Resources>
<DataTemplate DataType="{x:Type Bar}">
<Label Content="hello stackoverflow" />
</DataTemplate>
</ListBox.Resources>
</ListBox>
You could also set the ItemsTemplate directly by using <ListBox.ItemTemplate> instead of <ListBox.Resources>
See Data Binding Overview at MSDN.
First add your namespace to the Window element (Intellisense) :
xmlns:local="clr-namespace:yourenamespace"
Then the following XAML ( in Window.Resources is a clean way to do it ) :
<Window.Resources>
<ObjectDataProvider x:Key="DataProvider" ObjectType="{x:Type local:Foo}"/>
<DataTemplate x:Key="Template" >
<TextBlock Text="{Binding Bar}"/>
</DataTemplate>
</Window.Resources>
Place the Listbox :
<ListBox DataContext="{Binding Source={StaticResource DataProvider}}" ItemsSource="{Binding Bars}" ItemTemplate="DynamicResource Template" />
But, it depends on your code-behind object, you have to set a constructor to initialise public properties within your object which are ObservableCollection<> preferably (There is some restriction rules with object instance in XAML).

Resources