ListView SelectedItem does not get sychronized - wpf

I have following ListView and using MVVM Light
<ListView ItemsSource="{Binding Combinations, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Combination, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Style="{StaticResource UserValueStyle}" SelectionMode="Single"
IsSynchronizedWithCurrentItem="True"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Text}" FontSize="20" Height="80" Width="100" FontWeight="Bold"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.OnUserValuePressed}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The property:
public SapInspectionUserValue Combination
{
get
{
return _combination;
}
set
{
_combination = value;
RaisePropertyChanged();
}
}
The class property:
public class SapInspectionUserValue
{
public string Combination { get; set; }
public string Value { get; set; }
public string Text { get; set; }
}
My problem is, when I select an item(in my case, press the button) the SelectedItem does not get synchronized and I do not know why. Whats is wrong?

Related

wpf File Explorer change name of file inside ListView

Hello I have a model called FileDirectoryModel and it contains:
public class FileDirectoryModel
{
public string imgPath { get; set; }
public string DirPath { get; set; }
public string FileName { get; set; }
public string size { get; set; }
public string FileType { get; set; }
public DateTime Modifed { get; set; }
public string FullName { get; set; }
}
I also have a ListView :
<ListView x:Name="lstExplorer"
Background="White"
ItemsSource="{Binding DirectoryCollection}"
SelectedItem="{Binding SelectedModel}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding FileExplorerClickCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListView.View>
<GridView>
<GridViewColumn Header="Name"
Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding imgPath}"
Height="20"
Width="20"
Stretch="Fill"/>
<TextBlock x:Name="explorerText"
Text="{Binding FileName}">
</TextBlock>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="50">
<GridViewColumnHeader>
<TextBlock Text="Size" />
</GridViewColumnHeader>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding size}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100">
<GridViewColumnHeader>
<TextBlock Text="Type" />
</GridViewColumnHeader>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding FileType}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100">
<GridViewColumnHeader>
<TextBlock Text="Modifed" />
</GridViewColumnHeader>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Modifed}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
and DirectoryCollection is :
private ObservableCollection<FileDirectoryModel> _DirectoryCollection;
public ObservableCollection<FileDirectoryModel> DirectoryCollection
{
get { return _DirectoryCollection; }
set
{
if (_DirectoryCollection != value)
{
_DirectoryCollection = value;
OnPropertyChanged();
}
}
}
There is a dubbleclick command that will open the file that is selected but I'm having a hard time trying yo figure out how to allow someone to change the name of the selected item.
Please note that I saw File ListView, Change name which doesn't apply because they are telling how to change the file name not how to have xaml allow the change.
You will need to setup a CommandParameter to pass get the name of the double clicked item.
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding FileExplorerClickCommand}"
CommandParameter="{Binding}/>
</i:EventTrigger>
The FileDirectoryModel item that was double clicked will get passed to the FileExplorerClickCommand method (you will need to change the method params. You can then perform whatever you need on that item.

Treeview Bindings Not Showing Second level

Hello I have a treeview that I am trying to diplay 2 levels deep.
The second level is not displaying
Here is the class and Collection from my VM
public class BuildTree
{
public int Id { get; set; }
public string Groups { get; set; }
public IEnumerable<myAreas> Areas { get; set; }
}
//Areas has a name property I want to show on the second level under the group
public ObservableCollection<BuildTree> MyObsvCollection
{
get { return _myCollection; }
}
Here is my Xaml
<TreeView ItemsSource="{Binding MyObsvCollection}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Foreground="Red" Text="{Binding Groups}" />
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Areas}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Your HierarchicalDataTemplate has no items to display. You should move ItemsSource to outer HierarchicalDataTemplate and then ItemTemplate can be simple DataTemplate
<HierarchicalDataTemplate ItemsSource="{Binding Areas}">
<TextBlock Foreground="Red" Text="{Binding Groups}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>

Binding ItemsControl to ScrollViewer

At the begining I'll show some code:
private ObservableCollection<otwarteBezStolika> otwarteBezStolika = new ObservableCollection<otwarteBezStolika>();
public ObservableCollection<otwarteBezStolika> listunia
{
get { return otwarteBezStolika; }
set { otwarteBezStolika = value; }
}
}
public class otwarteBezStolika
{
public string number { get; set; }
public string date { get; set; }
public int orderID { get; set; }
public decimal price { get; set; }
public decimal priceR { get; set; }
public string opisRach { get; set; }
public string sum { get; set; }
}
And now in xaml:
<Window.Resources>
<DataTemplate x:Key="dataTempl">
<Border BorderBrush="Coral" BorderThickness="1" Width="170">
<Button Name="goToPOS" Tag="{Binding orderID}" Click="goToPOS_Click" Style="{StaticResource TabCloseButtonStyle}" Margin="1">
<StackPanel>
<Label Content="{Binding number}" HorizontalAlignment="Center" FontSize="15" FontWeight="ExtraBold" HorizontalContentAlignment="Center"></Label>
<Border BorderBrush="Turquoise" BorderThickness="1" Width="170"></Border>
<Label Content="{Binding date}" FontSize="12" HorizontalAlignment="Center"></Label>
<TextBlock Text="{Binding opisRach}" FontStyle="Italic" FontSize="12" Foreground="Black" TextAlignment="Center" TextWrapping="Wrap" Margin="0,0,0,2"></TextBlock>
<Border BorderBrush="WhiteSmoke" BorderThickness="1" Width="170"></Border>
<Label Content="{Binding sum}" FontSize="19" HorizontalAlignment="Center"></Label>
</StackPanel>
</Button>
</Border>
</DataTemplate>
<DataTemplate x:Key="mainTemplate">
<StackPanel>
<ItemsControl x:Name="imageContent" ItemsSource="{Binding listunia}" ItemTemplate="{StaticResource dataTempl}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</DataTemplate>
</Window.Resources>
Grid:
<Grid Grid.Row="0">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl Name="templ" ItemsSource="{Binding ElementName=UI, Path=listunia }" ItemTemplate="{StaticResource mainTemplate}" />
</ScrollViewer>
</Grid>
Problem is that I cannot see any item (I'm filling items using sqldatareader, and adding them to list - by the way, does DataTable will also work? So instead while(rdr.Read()) i could ouse SqlDataAdapter sda and sda.fill(Datatable))
Second problem is that, it does work when I put "dataTempl" inside scrollviewer ,like :
<Grid Grid.Row="0">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl Name="templ" ItemsSource="{Binding ElementName=UI, Path=listunia }" ItemTemplate="{StaticResource dataTempl}" />
</ScrollViewer>
</Grid>
but items are show vertically, but I need to see them from left to right horizontal.
Thanks for answers!
Try doing this.. you dont need maintemplate.
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl Name="templ" ItemsSource="{Binding ElementName=UI, Path=listunia }" ItemTemplate="{StaticResource dataTempl}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
OR you can simply use:
<ListBox Name="templ" ItemsSource="{Binding ElementName=UI, Path=listunia }" ItemTemplate="{StaticResource dataTempl}">
</ListBox>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl Name="kaloo" DisplayMemberPath="Name" ss:DragDrop.IsDragSource="True" ss:DragDrop.IsDropTarget="True" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ScrollViewer>

Multiple levels of binding in WPF ItemsControl

How can i bind multiple levels of data in same time like List of chapters and under each chapter list of pages.
The class structure and xaml i used is shown here
public class ContentsPage
{
public string cname{ get; set; }
public string label { get; set; }
}
public class Chapter
{
public string name { get; set; }
public string id { get; set; }
public List<ContentsPage> pages { get; set; }
}
public class Model
{
public List<Chapter> chapters { get; set; }
}
<ItemsControl x:Name="TopLevelListBox" ItemsSource="{Binding}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander ExpandDirection="Down" Width="175">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=name}" Margin="0,0,5,0"/>
</StackPanel>
</Expander.Header>
<ListBox x:Name="SubListBox" ItemsSource="{Binding Path=enrichments}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=cname}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
For binding i used the code
Model data = new Model(); //load data
TopLevelListBox.DataContext = data.chapters;
Only my expander headers are filled with result. What i need to do fill the pages inside the expander ? Any ideas or samples link for doing the same
Your ItemsControl Item template should change a bit. Instead of setting the ListBox as Content of Expander, set it as Content Template.
<Expander ExpandDirection="Down"
Width="175" Content="{Binding}">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=name}"
Margin="0,0,5,0" />
</StackPanel>
</Expander.Header>
<Expander.ContentTemplate>
<DataTemplate>
<ListBox x:Name="SubListBox"
ItemsSource="{Binding Path=enrichments}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=cname}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</Expander.ContentTemplate>
</Expander>
And make sure you have sub items to show in ListBox.

Group Style Header never appears

My GroupStyle Header never appears in the combobox....
The Grouping is working fine....It's sole a binding issue but am not able to figure out.
<ComboBox Height="23" Margin="33,45,125,0" Name="comboBox1" VerticalAlignment="Top" ItemsSource="{Binding}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<Border Background="Red">
<TextBlock Text="{Binding Path=value}" />
</Border>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock FontSize="12" FontWeight="Bold" Foreground="DarkGray">
<Button Content="{Binding Path=Location}"/>
<TextBlock Text="{Binding Path=Location}" />
<Button>bbbb</Button>
</TextBlock>
<ItemsPresenter/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ComboBox.GroupStyle>
</ComboBox>
and code behind
public class Store
{
public string Location { get; set; }
public string value { get; set; }
}
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
var myData = new ObservableCollection<Store>
{
new Store { Location = "Group1", value = "Item 1" },
new Store { Location = "Bombay", value = "Item 2" },
new Store { Location = "Group2", value = "Item 11" }
}
ICollectionView view = CollectionViewSource.GetDefaultView(myData);
view.GroupDescriptions.Add(new PropertyGroupDescription("Location"));
DataContext = myData;
}
}
Try changing the Binding Path from "Location" to "Name"
<GroupStyle.HeaderTemplate>
...
<Button Content="{Binding Path=Location}"/>
<TextBlock Text="{Binding Path=Location}" />
...
<GroupStyle.HeaderTemplate>
...like this...
<GroupStyle.HeaderTemplate>
...
<Button Content="{Binding Path=Name}"/>
<TextBlock Text="{Binding Path=Name}" />
...
<GroupStyle.HeaderTemplate>

Resources