How can i retreive "Header" of selected item in RadTreeView(SilverLight) control?
Assuming I understand correctly that you only want to get at header content (in this example simple text) and that you are using RadTreeViewItem as your tree nodes you could do something like this in your selection event response:
private void radTreeView1_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
{
foreach (RadTreeViewItem item in e.AddedItems)
{
string header = item.Header.ToString();
// Do soemthing with the header value here
}
}
This example allows for multiple selection mode, but works as well for a single selection.
Related
I have a grid which contains 2 rows and 2 columns. Each cell hosting a windows forms host control. I want to capture the selected cell when the user clicks on any of the cell. I have searched and found that there is no 'Click' event but I can make use of 'MouseDown' event with similar results. But now I am stuck because you would think there must be an easier way like 'GetCurentRow' and 'GetCurrentColumn' to capture the selected cell but there isn't.
What I want further to do is to get the child element in that particular cell of the grid.
I tried the following code but no matter which cell I click, I always get 0 for the row and column:
void InnerGridToContainWindowsFormsHost_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var element = (UIElement)e.Source;
int c = Grid.GetColumn(element);
int r = Grid.GetRow(element);
}
Is there any way to get the selected/clicked cell details or the children inside the cell?
I think that you may be going down the wrong route here... if you just want to know what control was clicked on, try using the VisualTreeHelper.HitTest method. You can find information on the VisualTreeHelper.HitTest Method page on MSDN. Basically, you would do something like:
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get the position of the mouse pointer relative to the grid
Point clickPoint = e.GetPosition(grid);
HitTestResult result = VisualTreeHelper.HitTest(grid, clickPoint);
if (result != null)
{
// Do something here
}
}
You may also need some kind of basic recursive function to drill down through the Visual objects until you get to the one you want.
I have a WPF DataGrid with a button on one of the columns.
When I click the button I have this function called:
MyClass mySC = (MyClass)(CollectionViewSource.GetDefaultView(grdMyClass.DataContext).CurrentItem);
This code works perfect, but when I click on the button on the new line (the last on on the grid) I get the msSC of the line before it, and not null, or something that related to the last new line.
how can I check if the button was clicked in the new line ?
If your intention is that clicking on a button should do something with the data bound to that row then just get the data from the button's data context. If null then user clicked on an empty row.
private void Button_Click(object sender, RoutedEventArgs e)
{
MyClass data = (sender as FrameworkElement).DataContext as MyClass;
}
I just got the exact same problem and created my own solution.
I have a DataGrid with ItemsSource binded to a list in my ViewModel, and SelectedIndex binded to an int in the viewmodel, in order to be able to play with the list when we choose something (and for example simulate an event such as "OnSelectionChanged")
So the solution is very simple here:
//App selected on the list
private int _selectedApp;
public int SelectedApp
{
get { return _selectedApp; }
set
{
if (value == _listApps.Count) _selectedApp = -1;
else _selectedApp = value;
OnPropertyChanged("SelectedApp");
}
}
I just check that the index isn't out of range: if it is, I set it to -1, so my app considers nothing is actually selected.
Hope this can help, feel free to ask more :)
I have a datagrid that displays a list of orders. Above the datagrid, there is a box that filters the orders by capturing the text input and then calling a server side method to get orders matching the supplied text. This works fine on the page.
I've tried adding a page that passes in parameters via querystring that would automatically apply a filter when the user lands on the page. The code to call the server side method is called correctly and the data is returned correctly, but the datagrid doesn't show the results. If I type the same text into the textbox on the page and search, the results are finally shown on the datagrid. It is almost like the datagrid doesn't see the change the first time the user comes to the page with an automatic filter. Here are some relavent bits:
protected override void OnNavigatedTo(NavigationEventArgs e) {
if (NavigationContext.QueryString.ContainsKey("filterkeyword") && NavigationContext.QueryString.ContainsKey("filtervalue")) {
string filterkeyword = NavigationContext.QueryString["filterkeyword"];
string filtervalue = NavigationContext.QueryString["filtervalue"];
switch (filterkeyword) {
default: ApplyDefaultFilter(filtervalue); break;
}
} else {
ApplyDefaultFilter("");
}
}
The datagrid is just bound to a data source object and the datasource is not set to auto load. Here are the bits for when a user does submit a search via the filter box:
private void txtFilter_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Enter) {
ApplyDefaultFilter(txtFilter.Text);
txtFilter.Select(0, txtFilter.Text.Length);
}
}
They both call the same methods and return the results, it is just that the textbox somehow tells the page to refresh the datagrid and magically the data appears. Is there an event I can force on the page to cause a refresh of the datagrid?
Does you datasouce object implement INotifyPropertyChanged or if it's a collection, is it an ObservableCollection?
The TextBox rebinds on changes AFAIK.
I added this and it seems to work...though this does seem like a bit of kludge.
void ordersDataSource_LoadedData(object sender, LoadedDataEventArgs e) {
ordersDataGrid.ItemsSource = e.Entities;
}
I didn't need it when filtering within the page, but redirecting the user to the page requires this....odd.
In project there is a DataGridView.
I have a little bit of code that I which displays information based on
the cell that was clicked.
My problem is how to detect if the user clicked on a column or row
header (anything other than a cell).
All this is tied to the 'dataGridView1_CellMouseDown' method, and I'm using
the HitTest to attempt to detect what the user clicked, but all I'm
getting is 'TopLeftHeader' when the user clicks a cell and 'None'
everywhere else and the Row index always comes as -1
Using the CellMouseDown event gives you coordinates relative to the cell that was clicked.
Use the control's MouseDown event instead, which will give you control-based coordinates.
See the example on MSDN.
You can still use the CellMouseDown event handler. In fact i find it a bit cleaner because with MouseDown event, you have to create a HitTest to get the selected row.
The following code is equivalent:
private void dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
// If right-click
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
// Get selected row
var selectedRow = dgvBatches.Rows[e.RowIndex];
}
}
private void dgv_MouseDown(object sender, MouseEventArgs e)
{
// If right-click
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
// Get the selected row/column
DataGridView.HitTestInfo info = dgvBatches.HitTest(e.X, e.Y);
// Get selected row
var selectedRow = dgvBatches.Rows[info.RowIndex];
}
}
I want to be able to pick a specific cell in a Silverlight 3.0 DataGrid and put it into edit mode. I can use the VisualTreeManager to locate the cell. How do I switch to edit mode?
Each DataGridCell looks like this in the VisualTreeManager:
System.Windows.Controls.DataGridCell
System.Windows.Controls.Grid
System.Windows.Shapes.Rectangle
System.Windows.Controls.ContentPresenter
System.Windows.Controls.TextBlock
System.Windows.Shapes.Rectangle
System.Windows.Shapes.Rectangle
with the TextBlock containing the text I want to edit.
Update
Following #AnthonyWJones' suggestion, here's how I tried to do this using BeginEdit().
I wanted to keep it simple so I thought I'd pick a column in the first row. Even that proved beyond my SL knowledge! In the end, I get the first row by creating a field called firstRow to hold it:
private DataGridRow firstRow;
added a LoadingRow handler to the DataGrid:
LoadingRow="computersDataGrid_LoadingRow"
and
private void computersDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
if (this.firstRow == null)
this.firstRow = e.Row;
}
and then adding a button to the panel to trigger the edit:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.dataGrid.SelectedItem = this.firstRow;
this.dataGrid.CurrentColumn = this.dataGrid.Columns[4];
this.dataGrid.BeginEdit();
}
I click the button and the correct cell is selected but it doesn't go into edit on the cell. It takes a manual click to achieve that.
I'm not sure why you need to find the DataGridCell using VisualTreeManager nor do I know currently how you would properly start editing . You may get away with simply setting the cell's visual state to editing.
VisualStateManager.GoToState(myDataGridCell, "Editing", true);
I'm not sure how the grid behaves when you do something like the above. You may find things goe a bit pearshaped if you need DataGrid to help you revert changes to a row.
The "standard" approach would be to set the DataGrid SelectedItem property to the item represented by the row, set the CurrrentColum property to the DataGridColumn object that represents to the column in which the cell is found. Then call the BeginEdit method.
I am not able to understand your problem properly, but I had a similar problem
I wanted to make only few of the Grid Cells editable and rest were not. Instead of creating a logic and assigning ReadOnly as true/ false, I did the simple thing.
Mark the whole Grid's cells are writable, IsReadOnly as false
Set the event PreparingCellForEdit and send a callback
When you double click on a cell, it gets in the edit mode
Check whether this cell you want to be editable
If it is allowed to be edited, go ahead
If that cell is ReadOnly, then call CancelEdit
The sample code goes like
namespace foo
{
public class foobar
{
public foobar()
{
sampleGrid = new DataGrid();
sampleGrid.IsReadOnly = false;
sampleGrid.PreparingCellForEdit += new EventHandler<DataGridPreparingCellForEditEventArgs>(sampleGrid_PreparingCellForEdit);
}
void sampleGrid_PreparingCellForEdit(object sender, DataGridsampleGrid_PreparingCellForEditEventArgs e)
{
if (sampleGrid.SelectedItem != null)
{
bool isWritableField = CheckIfWritable()
if (isWritableField == false)
{
sampleGrid.CancelEdit();
}
// continue with your logic
}
}
private DataGrid sampleGrid;
}
}