How to bind a Dictionary<enum,bool> property to a GridViewColumn? - c#

I am trying to bind a Dictionary property stored in a EffectViewModel inside an ObservableCollection, but it appears as "(Collection)" in the column I want to see as checkboxes.
Xaml for the GridViewColumn is this:
<GridViewColumn
Width="100"
Header="GPU"
DisplayMemberBinding="{Binding ShaderSupport}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0"
HorizontalAlignment="Center"
IsChecked="{Binding Value}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
ShaderSupport is of type Dictionary<ShaderType, bool> where I just want to read in the bool value for ShaderType.GPU for this GridViewColumn.
Any ideas?
EDIT: Using this shows me the bool value as string, so I am in the right path I think:
DisplayMemberBinding="{Binding ShaderSupport[GPU]}">

Try this:
<GridViewColumn
Width="100"
Header="GPU">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0"
HorizontalAlignment="Center"
IsChecked="{Binding ShaderSupport[CPU]}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

Related

showing multiple items inside listview

I was wondering if there is a way to show multiple elements inside listview.view.
SampleCode (which doesnst work)
<ListView
x:Name="Somename"
ItemsSource="{Binding someObservableCollection}">
<ListView.View>
<TextBlock></TextBlock>
<TextBlock></TextBlock>
<TextBlock></TextBlock>
<GridView>
<GridViewColumn Header="Column 1" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Column 2">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Sample Data properties for a person.
Name
Age
Id
OldData1
OldData2
OldData3
NewData1
NewData2
NewData3
I am trying to show Name, age, Id and 3 textblocks
and then in a grid, compare olddata vs new data.
I tried doing with itemtemplate and then doing stackpanel with grid inside, but that grid looks very basic and would prefer gridview since it looks better.

WPF ListView - 2 dimensions with 1 same structure

We are working on a ListView (C# WPF) and we didn't found how bind a list of items in columns, with each item containing itself a list of items with the same columns.
Let's illustre this in an example :
!
We got an observable collection on objects with parameters (name, etc.) and each object contain another observable collection of objects with the sames parameters (exept they haven't a list). So we want to list it in a ListView but we can't figure how !
We do not know enough ListView to implement this structure, some advices ?
Thanks in advance
I think the best way is :
XAML :
<Window.Resources>
<DataTemplate x:Key="gridViewSecondCellTemplate1">
<StackPanel Width="100">
<TextBlock Text="{Binding Content}" FontSize="15" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="gridViewCellTemplate1">
<StackPanel Width="100">
<TextBlock Text="{Binding Title}" FontSize="15" />
<ListView ItemsSource="{Binding MySecondSource}>
<ListView.View>
<GridView>
<GridViewColumn Header="{Binding Subtitle}" CellTemplate="{StaticResource gridViewSecondCellTemplate1}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</DataTemplate>
</Window.Resources>
<ListView ItemsSource="{Binding MySource}">
<ListView.View>
<GridView>
<GridViewColumn Header="Col 1" Width="100" CellTemplate="{StaticResource gridViewCellTemplate1}"/>
</GridView>
</ListView.View>
</ListView>
I didn't try this code. Try it, and say me if it is OK.

How to prevent listview items checkbox to be de-selected clicking out of checkboxes?

I've been messing around with checkboxes and listview in my wpf application. Selecting multiple items seems to work properly, but when I click on an item and not on its checkbox it selects the item and deselects all the others previously selected, how can I fix this ?
Here's my code:
<ListView Name="NewPlace_ActionsList" HorizontalAlignment="Left" Height="254" Margin="10,74,0,0" VerticalAlignment="Top" Width="389" >
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding Id}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Codice" DisplayMemberBinding="{Binding Path=Id}" />
<GridViewColumn Header="Nome" DisplayMemberBinding="{Binding Path=Name}" />
<GridViewColumn Header="Descrizione" DisplayMemberBinding="{Binding Path=Desc}" />
</GridView>
</ListView.View>
</ListView>
What do you want to accomplish? The system behaves exactly to what you coded: the checkbox is selected only in the selected row. Because the Checkbox "follows" the IsSelected of the row (ListViewItem).

Add multiple items in a listbox row

So far i have been working with listview without any problem. I have been able to pass multiple columns and fill them with images, text etc. I decided to do the same thing with a listbox but things there got complicated.
In a listview i do the following :
<DataTemplate x:Key="ImageTemplate" >
<Image x:Name="FavoriteImageList" Tag="{Binding tagger}" Width="12" Height="12" Source="{Binding ImageSource}" MouseLeftButtonDown="FavoriteImageList_MouseLeftButtonDown"/>
</DataTemplate>
<ListView x:Name="Citys">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
<GridViewColumn Width="90" Header="Image" CellTemplate="{StaticResource ImageTemplate}"/>
<GridViewColumn Width="178" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="178" Header="City" DisplayMemberBinding="{Binding City}"/>
<GridViewColumn Width="140" Header="Author" DisplayMemberBinding="{Binding Author}"/>
</GridView>
</ListView.View>
</ListView>
And i fill columns like this :
foreach(var fav in Favorites)
{
Citys = new List<ACity>()
{
new ACity() {tagger = Name ,ImageSource = ImageSource ,Name= Name, Author= Author, City = City}
};
ListBox.Items.Add(Citys);
}
If I do this in a listbox, it will fill with (Collection).
Is there a way to do this in a listbox, passing images buttons strings in a listbox row?
See the example from the MSDN ItemsControl.ItemTemplate
<ListBox Width="400" Margin="10"
ItemsSource="{Binding Source={StaticResource myTodoList}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
It should be pretty straight forward to match with your example.

Checkbox in gridviewcolumn(header)

I am using a listview (gridview/gridviewcolumn)
where the first column
contains only checkboxes for each row. Instead of adding a select all button I want
to add a Checkbox into the header of the first column.
Selecting the checkbox in the header will select
all other checkboxes and vice versa.
How can I do that in xaml?
Update:
This is the important part of my xaml code. (simplified)
<ListView ItemSource="...">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
To have a checkbox on top of GridViewColumn you can do something like this
<GridViewColumn>
<GridViewColumn.Header>
<CheckBox/>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Now you can handle the check event and in the code you can iterate through your itemsSource and change values or if you are following MVVM you can bind property with checkbox so that whenever check is changed you will be notified through INotifyPropertyChanged. Once you find out through binding that check has changed, again you can change your itemssource accordingly

Categories