What does the I in ICommand or INotifyPropertyChanged stand for?
WPF DATAGRID UPDATE AND DELETE
Hi,
Am very new to wpf datagrid ....i want to add, edit, delete a value from DB.
ADD:Finished
EDIT and DELETE:
I placed 2 buttons..and i dont know how to do that can any one guide me on this.
Point 1: I want to display my name vale from lable control to textbox control
Point2: Then edit the same..am using LINQ for my project, and also am using oneway binding mode.
<DG:DataGrid ItemsSource="{Binding}"
AutoGenerateColumns="False" Name="dgview">
<DG:DataGrid.Columns>
<DG:DataGridTemplateColumn Header="NAME">
<DG:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding empname,Mode=OneWay}" Name="lblname" Width="100"></Label>
</DataTemplate>
</DG:DataGridTemplateColumn.CellTemplate>
<DG:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Name="txtname"></TextBox>
</DataTemplate>
</DG:DataGridTemplateColumn.CellEditingTemplate>
</DG:DataGridTemplateColumn>
<DG:DataGridTemplateColumn Header="EDIT">
<DG:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Edit" Name="btn_edit" Click="btn_edit_Click">
</Button>
</DataTemplate>
</DG:DataGridTemplateColumn.CellTemplate>
<DG:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
</DataTemplate>
</DG:DataGridTemplateColumn.CellEditingTemplate>
</DG:DataGridTemplateColumn>
<DG:DataGridTemplateColumn Header="DELETE">
<DG:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" Name="btn_delete" Click="btn_delete_Click"></Button>
</DataTemplate>
</DG:DataGridTemplateColumn.CellTemplate>
<DG:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
</DataTemplate>
</DG:DataGridTemplateColumn.CellEditingTemplate>
</DG:DataGridTemplateColumn>
</DG:DataGrid.Columns>
</DG:DataGrid>
Thanks SABARINATHAN87
Cannot move button inside a xamdockmanager
Hi,
I have a xamdockmanager in a grid and a button in xamdockmanager. Button can be only moved vertically. I cannot locate the button on its correct position. How to achieve this.
Thanks
Winform UserControl's gradient background not displayed when in WPF component... why??
Hello,
We have a Winforms application that we are looking to convert to WPF. For the time being we are building a WPF Window shell and in one of the areas we are simply bringing one of our Winform UserControls. The xaml looks like:
<Grid>
<WindowsFormsHost>
<wf:MyComponent x:Name="aia"/>
</WindowsFormsHost>
</Grid>
This is a UserControl where we use a gradient for the background:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
LinearGradientBrush lb = new LinearGradientBrush(this.ClientRectangle, Color.LightBlue, Color.Blue, LinearGradientMode.Horizontal);
e.Graphics.FillRectangle(lb, this.ClientRectangle);
lb.Dispose();
}
This works fine when the UserControl was within our Winform's application but when it is in our WPF application, the gradient does not get rendered although I can see that the method is being called.
Does anyone know why it isn't getting rendered within a WPF control? Thanks for any thoughts.
- Peter
Focus() does not always appear to work for me ...
{usercontrol (wpf)} listbox datasource and data template
dear all
i'm create this project with vb.net 2010, and i wish create some listbox data template with usercontrol (wpf).
this picture hope just anyone can understand what i want:
can anyone give me some advice or some project can i follow ??
XamlReader.Load throws error : Item has already been added. Key in dictionary: 'NewBrushKey' Key being added: 'NewBrushKey'
Hi,
I am getting the below error while i am cloning my WPF control using XamlReader.Load method.
XamlReader.Load throws error : Item has already been added. Key in dictionary: 'NewBrushKey' Key being added: 'NewBrushKey'
XAML:
<UserControl x:Class="TestProject.UIControl">
<UserControl.Resources>
<SolidColorBrush x:Key="NewBrushKey" Color="Red" />
</UserControl.Resources>
<Border BorderBrush="{StaticResource NewBrushKey}" BorderThickness="2">
<Grid>
...
...
...
</Grid>
</Border>
</UserControl
//User control is reused in this window.
<Window>
<Grid x:Name="UIControlContainer">
...
...
<ctrl:UIControl x:Name="firstUIControl" />
<ctrl:UIControl x:Name="secondUIControl" />
</Grid>
<Window>
C#:
//UIControlContainer object is closed using XAML Parser in code behind.
string _containerStr = XamlWriter.Save(UIControlContainer);
StringReader _strReader = new StringReader(_containerStr);
XmlTextReader _xmlReader = new XmlTextReader(_strReader);
UIElement _containerClone = (UIElement)XamlReader.Load(_xmlReader);
But XamlReader.Load(_xmlReader) method throws "Item has already been added. Key in dictionary: 'NewBrushKey' Key being added: 'NewBrushKey'"
Please help me how to solve this problem.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
Disable Windows 8 touch keyboard emoticons/emoji characters?
Windows 8 has the on screen touch keyboard that allows entry of character glyph emoticons. These characters are causing my app to crash when the text gets parsed in my C++ CLR layer beneath. Is there a way to disable these characters from entry in my TextBoxes?
WPF Datagrid: When Frozen Columns, add any control at the Horizontal Scrollbar's blank area
Hi,
I have a FrozenColumnCount
= 3
always with HorizontalScrollBar
visible.
Now, I have a requirement to add controls in the blank area (left of the scroll bar) -- See the RED highlighted area in the image.
How can I add any control there ? It will be great if something of this sort can be achieved via XAML (ControlTemplates) ? I am using the standard WPF
v4.0 datagrid
in my application.
NB: The sample can be downloaded from here -http://blogs.msdn.com/b/vinsibal/archive/2008/10/31/wpf-datagrid-frozen-row-sample.aspx
a WPF XBAP project compiling issue
Hi, all:
I found my WPF XBAP project take ten minutes on compiling, but it at most ten seconds before. I create a new empty WPF XBAP project, I got the same issue. I tested this issue with VS2008, VS2010, who can tell me why?
how to bind textblock using MVVM in wpf?
Hi all
i am creating WPF app which is based on MVVM model.
in my app i am using two different classes which have inherited by same parent class as bellow:
Public class CARVM : Viewmodel
{
//some properties
//Some methods
}
and
Public class BikeVM : Viewmodel
{
//some properties
//Some methods
}
and now i am assigning class CARVM as a datacontext of my XAML file.
Further process is as below :
1.calling one method of BikeVM from code behind(XAML.cs).
2.In method(BikeVM method) i am assigning a value to parent's class property(i.e Viewmodel's Property)
3. now i am binding this property to control in XAML class as below:
<TextBlock Text="{Binding Test,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
while debugging i am getting the new value of properety but in xaml it is not going to bind.
please help me out.
Thanks in advance.
sumitk
binding a label to an underlying data structure
Converter.Convert not being called
I am using VS2008. I am using a style trigger to change the background and tooltip of a textbox when there is an error. The Setter.Value for Property="ToolTip" used to be
<Binding RelativeSource="{x:Static RelativeSource.Self}" Path="(Validation.Errors)[0].ErrorContent" />
It basically worked but I was getting debugging errors because Validation.Errors would be empty. I tried this:
<Binding RelativeSource="{x:Static RelativeSource.Self}" Path="Validation.Errors" Converter="{StaticResource ves2obj}" />
The Convert method is not being called. Does anyone see what I am doing wrong?
Bill Swartz
Windows From's control are getting distorted while hosting them in WindowsFromsHost in WPF application
Hi,
I am currently migrating an existing win form C# application to WPF. So we are not rewriting existing WinForm, instead we are showing existing winform dialogs to user using WindowsFormsHost class. While doing so, dialog looks fine with Windows7 small settings [Control Panel->Display->Small] while if i open the same dialog on Medium display settings of Win7 OS then dialogs' control's are distorted and overlapped. While existing winform application shows dialogs proper in both display settings.
Can you please guide me how to correct the same in WPF? Sample code which i am using to show existing win form on WPF window is below -
WindowsFormsHost hostForm = new WindowsFormsHost();
//WinForm object
WinForm1 frmObj= new WinForm1 ();
frmObj.TopLevel = false;
hostForm.Child = frmObj;
this.grid1.Children.Add(hostForm); //grid1 is control in WPF window
Any help would be highly appreciable.
ITChamp
Problem in CrystalReportViewer.load
Hello
i added report viewer in my WPF window and add a "crystalreport.rpt" in root of my project
in code behind i add this
markazbam018Entities db = new markazbam018Entities(); public MainWindow() { InitializeComponent(); CrystalReport1 cr1 = new CrystalReport1(); ReportDocument report = new ReportDocument(); report.Load("CrystalReport1.rpt"); report.SetDataSource(from r in db.PersonnelPaidItemSalaries where r.f_PersonCode == "F8FAF927E190C" select r); CRViewr.ViewerCore.ReportSource = report; }
but in load i get error
whats my mistake ?
thanks
Hossein Aftabi http://www.internationalsell.com
Requested registry access is not allowed when using elementhost in windows forms as impersonated user
I'm using VS 2012 for maintaining windows forms application. Recently we upgraded to .NET 4.0 framework and started using some WPF controls. The problem is when impersonated user is logged in an error message "Requested registry access is not allowed" is displayed. This happens on ElementHost control initialization. When the application user is the same as Windows user there is no error. The problem is evident on XP and Windows 7.
Correct way to reference child element in control from code
Hi,
I'm trying to build a custom control that is a textbox that displays recent similar values. I've seen a couple approaches to this, but not with XAML. I'm pretty new to custom controls, so this may be a simple question, but I haven't found an answer yet.
The problem I'm trying to solve is how to capture the down-arrow key and transfer focus from the textbox to the popup with results.
<ScrollViewer x:Name="PART_ContentHost" Grid.Column="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /><Popup><Popup.Style><Style TargetType="Popup"><Setter Property="IsOpen" Value="True"/><Style.Triggers><DataTrigger Binding="{Binding History.Matches, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SearchTextBox}}}" Value="{x:Null}"><Setter Property="IsOpen" Value="False"/></DataTrigger><DataTrigger Binding="{Binding IsFocused, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SearchTextBox}}}" Value="False"><Setter Property="IsOpen" Value="False"/></DataTrigger><DataTrigger Binding="{Binding HasText, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SearchTextBox}}}" Value="False"><Setter Property="IsOpen" Value="False"/></DataTrigger></Style.Triggers></Style></Popup.Style><ListBox Padding="2" x:Name="ctlMatchesListBox" ItemsSource="{Binding History.Matches, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SearchTextBox}}}" /></Popup>
The ctlMatchesListBox isn't available in the code-behind, and I'm not sure how to "find" the ListBox to set the selected item to the the first on the list in the event handler for the down arrow. What is the "correct" way to transfer focus here?
Thanks,
Terry
how_to_add_scrolling_functionality_to_a_frame
Hi,
is it possible to add scrollview to a frame?
Because I've tried in this way
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"><Frame Name="myFrame" ScrollViewer.CanContentScroll="True" HorizontalAlignment="Left" Margin="10,10,0,-306.764" Width="752.685"/></ScrollViewer>
and it's doesn't work.. Am I wrong something? Where/What?
Thank in advanced
Ludo
Trying to put a list of expanders inside a combobox
Hello,
I am trying to display a list of expanders in a combo box. The basic functionality is there and working as expected. The combo box items source is bound to a dictionary<string, list<data>>.
However, there are a few issues that are problematic. If you run the example code, Item B contains 10 subitems, Item C contains 20 subitems and Item G contains 30 subitems.
- For items with a large number of subitems, when the expander is opened, the expander list box displays a vertical scrollbar but no thumb
- If you click in an expander list box vertical scrollbar area, the combo box closes
- When scrolling the combo box vertical scrollbar, I would like it to honor expander subitems when scrolling - currently it just scrolls to the next outer item (eg Item J, Item K, etc)
- If a subitem is clicked within an expanded expander, I would like the combo box to display that when the popup is closed (eg Item A - SubItem 4)
Any help or suggestions would be greatly appreciated.
Here is the xaml
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"><Grid><StackPanel Margin="5"><ComboBox Name="comboBox1" Margin="5"><ComboBox.ItemTemplate><DataTemplate><Expander Header="{Binding Key}"><ScrollViewer Name="PART_ScrollViewer" VerticalScrollBarVisibility="Visible"><ListBox HorizontalAlignment="Stretch" ItemsSource="{Binding Value}"><ListBox.ItemTemplate><DataTemplate><TextBlock Text="{Binding Name}" ToolTip="{Binding Description}" /></DataTemplate></ListBox.ItemTemplate></ListBox></ScrollViewer></Expander></DataTemplate></ComboBox.ItemTemplate></ComboBox></StackPanel></Grid></Window>And here is the code behind. There is nothing of interest here. All it does it build the dictionary.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); comboBox1.ItemsSource = getItems(); } private Dictionary<string, List<Data>> getItems() { Dictionary<string, List<Data>> items = new Dictionary<string, List<Data>>(); items.Add("Item A", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item B", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, new Data { Name = "SubItem 6", Description = "Item 6 Description" }, new Data { Name = "SubItem 7", Description = "Item 7 Description" }, new Data { Name = "SubItem 8", Description = "Item 8 Description" }, new Data { Name = "SubItem 9", Description = "Item 9 Description" }, new Data { Name = "SubItem 10", Description = "Item 10 Description" }, }); items.Add("Item C", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, new Data { Name = "SubItem 6", Description = "Item 6 Description" }, new Data { Name = "SubItem 7", Description = "Item 7 Description" }, new Data { Name = "SubItem 8", Description = "Item 8 Description" }, new Data { Name = "SubItem 9", Description = "Item 9 Description" }, new Data { Name = "SubItem 10", Description = "Item 10 Description" }, new Data { Name = "SubItem 11", Description = "Item 11 Description" }, new Data { Name = "SubItem 12", Description = "Item 12 Description" }, new Data { Name = "SubItem 13", Description = "Item 13 Description" }, new Data { Name = "SubItem 14", Description = "Item 14 Description" }, new Data { Name = "SubItem 15", Description = "Item 15 Description" }, new Data { Name = "SubItem 16", Description = "Item 16 Description" }, new Data { Name = "SubItem 17", Description = "Item 17 Description" }, new Data { Name = "SubItem 18", Description = "Item 18 Description" }, new Data { Name = "SubItem 19", Description = "Item 19 Description" }, new Data { Name = "SubItem 20", Description = "Item 20 Description" }, }); items.Add("Item D", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item E", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, new Data { Name = "SubItem 6", Description = "Item 6 Description" }, new Data { Name = "SubItem 7", Description = "Item 7 Description" }, new Data { Name = "SubItem 8", Description = "Item 8 Description" }, new Data { Name = "SubItem 9", Description = "Item 9 Description" }, new Data { Name = "SubItem 10", Description = "Item 10 Description" }, }); items.Add("Item F", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, new Data { Name = "SubItem 6", Description = "Item 6 Description" }, new Data { Name = "SubItem 7", Description = "Item 7 Description" }, new Data { Name = "SubItem 8", Description = "Item 8 Description" }, new Data { Name = "SubItem 9", Description = "Item 9 Description" }, new Data { Name = "SubItem 10", Description = "Item 10 Description" }, new Data { Name = "SubItem 11", Description = "Item 11 Description" }, new Data { Name = "SubItem 12", Description = "Item 12 Description" }, new Data { Name = "SubItem 13", Description = "Item 13 Description" }, new Data { Name = "SubItem 14", Description = "Item 14 Description" }, new Data { Name = "SubItem 15", Description = "Item 15 Description" }, new Data { Name = "SubItem 16", Description = "Item 16 Description" }, new Data { Name = "SubItem 17", Description = "Item 17 Description" }, new Data { Name = "SubItem 18", Description = "Item 18 Description" }, new Data { Name = "SubItem 19", Description = "Item 19 Description" }, new Data { Name = "SubItem 20", Description = "Item 20 Description" }, }); items.Add("Item G", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, new Data { Name = "SubItem 6", Description = "Item 6 Description" }, new Data { Name = "SubItem 7", Description = "Item 7 Description" }, new Data { Name = "SubItem 8", Description = "Item 8 Description" }, new Data { Name = "SubItem 9", Description = "Item 9 Description" }, new Data { Name = "SubItem 10", Description = "Item 10 Description" }, new Data { Name = "SubItem 11", Description = "Item 11 Description" }, new Data { Name = "SubItem 12", Description = "Item 12 Description" }, new Data { Name = "SubItem 13", Description = "Item 13 Description" }, new Data { Name = "SubItem 14", Description = "Item 14 Description" }, new Data { Name = "SubItem 15", Description = "Item 15 Description" }, new Data { Name = "SubItem 16", Description = "Item 16 Description" }, new Data { Name = "SubItem 17", Description = "Item 17 Description" }, new Data { Name = "SubItem 18", Description = "Item 18 Description" }, new Data { Name = "SubItem 19", Description = "Item 19 Description" }, new Data { Name = "SubItem 20", Description = "Item 20 Description" }, new Data { Name = "SubItem 21", Description = "Item 21 Description" }, new Data { Name = "SubItem 22", Description = "Item 22 Description" }, new Data { Name = "SubItem 23", Description = "Item 23 Description" }, new Data { Name = "SubItem 24", Description = "Item 24 Description" }, new Data { Name = "SubItem 25", Description = "Item 25 Description" }, new Data { Name = "SubItem 26", Description = "Item 26 Description" }, new Data { Name = "SubItem 27", Description = "Item 27 Description" }, new Data { Name = "SubItem 28", Description = "Item 28 Description" }, new Data { Name = "SubItem 29", Description = "Item 29 Description" }, new Data { Name = "SubItem 30", Description = "Item 30 Description" }, }); items.Add("Item H", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item I", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item J", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item K", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item L", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item M", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item N", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item O", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item P", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item Q", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item R", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item S", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item T", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item U", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item V", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item W", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item X", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item Y", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); items.Add("Item Z", new List<Data> { new Data { Name = "SubItem 1", Description = "Item 1 Description" }, new Data { Name = "SubItem 2", Description = "Item 2 Description" }, new Data { Name = "SubItem 3", Description = "Item 3 Description" }, new Data { Name = "SubItem 4", Description = "Item 4 Description" }, new Data { Name = "SubItem 5", Description = "Item 5 Description" }, }); return items; } } public class Data { public string Name { get; set; } public string Description { get; set; } } }
DataGrid ScrollIntoView does not bring given item into view (when using virtualization and grouping)
Hi All,
I use the DataGrid from .Net Framework 4.5 with row and column virtualization enabled. If I select an item in the DataGrid, add grouping using the DataGrid GroupStyle property and then execute a ScrollIntoView for the selected item, the selected item is not brought into view. I need the selected item to be brought into view programatically after adding the grouping without any interaction from the user.
Steps to reproduce:
- compile the code provided and start the application
- select the item with FirstName equal to "FirstName0000000030"
- press button "Add group"
- groups appear but the selected item is not brought into view
Notes:
- the issue does not reproduce for all items in the grid (for some it does, for some it doesn't). On my machine it allways reproduces with the item "FirstName0000000030". If it does not reproduce please try also with other items.
- pressing the button "Scroll to selection" multiple times after the "Add group" is pressed it finally bring the selected element into view (this is not a desireable solution for us because it involve user interaction from the user)
XAML:
<Window x:Class="ScrollIntoViewBug.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local ="clr-namespace:ScrollIntoViewBug" Title="ScrollIntoView" WindowState="Maximized" Loaded="Window_Loaded"><Window.Resources><GroupStyle x:Key="GroupHeaderStyle"><GroupStyle.Panel><ItemsPanelTemplate><VirtualizingStackPanel Orientation="Vertical" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Standard"/></ItemsPanelTemplate></GroupStyle.Panel><GroupStyle.ContainerStyle><Style TargetType="{x:Type GroupItem}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type GroupItem}"><Expander Name="groupExpander" IsExpanded="True"><Expander.Header><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Path=ItemCount}"/><TextBlock Text="Items"/></StackPanel></Expander.Header><ItemsPresenter></ItemsPresenter></Expander></ControlTemplate></Setter.Value></Setter></Style></GroupStyle.ContainerStyle></GroupStyle></Window.Resources><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="*"></ColumnDefinition></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="30"></RowDefinition><RowDefinition Height="*"></RowDefinition></Grid.RowDefinitions><StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal"><Button Name="btnAddGrouping" Click="btnAddGrouping_Click" Margin="0,3,3,3">Add group</Button><Button Name="btnClearGrouping" Click="btnClearGrouping_Click" Margin="3">Clear group</Button><Button Name="btnScrollIntoView" Click="btnScrollIntoView_Click" Margin="3">Scroll to selection</Button></StackPanel><DataGrid Name="MyDataGrid" Grid.Row="1" Grid.Column="0" EnableRowVirtualization="True" EnableColumnVirtualization="True" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingPanel.IsVirtualizingWhenGrouping="True" VirtualizingPanel.VirtualizationMode="Standard"></DataGrid></Grid></Window>
Code behind:
namespace ScrollIntoViewBug { public enum Gender { Male, Female }; public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public Gender Gender { get; set; } public string Department { get; set; } } public partial class MainWindow : Window { CollectionViewSource cvSource; public MainWindow() { InitializeComponent(); } private List<Customer> createRows() { System.Int64 rowNo = 100; System.Int64 noOfGroups = 80; // generate rows List<Customer> customers = new List<Customer>(); for (System.Int64 i = 0; i < rowNo; i++) { Gender gender; if (i % 2 == 0) gender = Gender.Male; else gender = Gender.Female; string deparment = "Dept" + (i % noOfGroups + 1).ToString("D10"); customers.Add(new Customer() { FirstName = "FirstName" + (i + 1).ToString("D10"), LastName = "LastName" + i.ToString("D10"), Gender = gender, Department = deparment }); } return customers; } private ObservableCollection<DataGridColumn> createColumns() { System.Int64 colNo = 100; // generate columns ObservableCollection<DataGridColumn> columns = new ObservableCollection<DataGridColumn>(); DataGridTextColumn textColumn = new DataGridTextColumn(); textColumn.Header = "First Name"; textColumn.Binding = new Binding("FirstName"); columns.Add(textColumn); DataGridComboBoxColumn comboColumn = new DataGridComboBoxColumn(); comboColumn.Header = "Gender"; comboColumn.TextBinding = new Binding("Gender"); comboColumn.ItemsSource = typeof(Gender).GetEnumValues(); columns.Add(comboColumn); DataGridTextColumn depColumn = new DataGridTextColumn(); depColumn.Header = "Department"; depColumn.Binding = new Binding("Department"); columns.Add(depColumn); for (System.Int64 i = 2; i < colNo; i++) { textColumn = new DataGridTextColumn(); textColumn.Header = "Last Name" + (i + 1).ToString("D10"); textColumn.Binding = new Binding("LastName"); columns.Add(textColumn); } return columns; } private void loadGridData() { // add columns ObservableCollection<DataGridColumn> columns = createColumns(); foreach (DataGridColumn col in columns) MyDataGrid.Columns.Add(col); // add rows cvSource = new CollectionViewSource(); cvSource.Source = createRows(); // set datagrid items Binding itemsSourceBinding = new Binding(); itemsSourceBinding.Source = cvSource; MyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, itemsSourceBinding); } private void Window_Loaded(object sender, RoutedEventArgs e) { loadGridData(); } private void btnAddGrouping_Click(object sender, RoutedEventArgs e) { ICollectionView view = CollectionViewSource.GetDefaultView(MyDataGrid.ItemsSource); if (view == null) return; view.GroupDescriptions.Add(new PropertyGroupDescription("Department")); if (MyDataGrid.GroupStyle == null || MyDataGrid.GroupStyle.Count == 0) { GroupStyle groupStyle = TryFindResource("GroupHeaderStyle") as GroupStyle; if (groupStyle != null) { if (groupStyle.ContainerStyle != null) groupStyle.ContainerStyle.Seal(); MyDataGrid.GroupStyle.Add(groupStyle); } } ScrollToSelection(); } private void ScrollToSelection() { MyDataGrid.Focus(); MyDataGrid.Items.MoveCurrentTo(MyDataGrid.SelectedItem); MyDataGrid.ScrollIntoView(MyDataGrid.SelectedItem); } private void btnClearGrouping_Click(object sender, RoutedEventArgs e) { ICollectionView view = CollectionViewSource.GetDefaultView(MyDataGrid.ItemsSource); if (view == null) return; view.GroupDescriptions.Clear(); ScrollToSelection(); } private void btnScrollIntoView_Click(object sender, RoutedEventArgs e) { ScrollToSelection(); } } }
Do I use correctly the ScrollIntoView in the code example?
Is there a way to programmaticaly scroll to the selected item imediatly after adding the grouping?
Is this a bug in the DataGrid from .Net Framework 4.5 related to ScrollIntoView?