How can i get the clicked button's row values on silverlight telerik radgridview button column? - silverlight

I have a hyperlink button colum in silverlight telerik radgridview, as a cell template.
I am using MVVM.
When i click on the hyperlink button, how can i get its row's values(atleast in the code behind)..?
Thanks in advance..

You need to use the ParentOfType extension method to get hold of the row containing the button.
Once you have the row you can cast the item type to your viewmodel and access the properties of it.
Make sure you have a using statement for the extension method:
using Telerik.Windows.Controls;
private void Button_Click(object sender, RoutedEventArgs e)
{
var parent = (sender as Button).ParentOfType<GridViewRow>();
var rowVM = parent.Item as YourVmClass;
}
If you are using MVVM you can also use the EventToCommandBehavior.
This allows you to execute all events as commands. You can then handle the command in your viewmodel.

Related

WPF: how to auto scroll to my first ListViewItem when form load

So i am build simple Clipboard manager.
Every ListViewItem come from Clipboard.GetText and my application minimize to Tray and when double click on its Icon the application jump and i want to focus become on the first ListViewItem in order to be able to navigate with Up & Down arrows.
This is my ListView:
ListView myListView;
Model List:
public ObservableCollection<string> Clipboards
Window Loaded event:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (viewModel.Clipboards.Count != 0)
myListView.ScrollIntoView(myListView.Items[0]);
myListView.Focus();
}
So currently when i open int the first time the application, the Focus is not on any ListViewItem and in the next time the focus is on the last selected/click ListViewItem and not on the first one.
It looks like you have your ViewModel in the code behind. This is not good MVVM standard.
Maybe this could be helpful
How can I set the focus to a ListBox properly on load if it uses databinding?
From what I see you are not focusing any ListViewItem but the ListView itself. I think this is your mistake. To focus the item you have to get it's container. The objects in the ItemsSource are actually the data itself and no the UIElement to render. To draw this data or add it to the visual tree for rendering, the ItemsControl will generate a container for the data e.g. a ListViewItem. Only the UIElement can receive focus, that's why the UIElement exposes the Focus() method. You have to use the ItemContainerGenarator to retrieve this container for your data:
ListView myListView;
(myListView.ItemsPanel as VirtualizingPanel)?.BringIndexIntoViewPublic(0);
myListView.Dispatcher.Invoke(new Action(
() =>
{
ListBoxItem dataContainer = myListView.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
dataContainer?.Focus();
}), DispatcherPriority.ContextIdle);
This example will move the focus to the first element in the ListView.

how to fetch data from a dynamically generated usercontrol containing a listview

I have a usercontrol that contains a listview. Inside this listview I set the listvidew.itemsource = table. Then I create a gridview and add the gridview to the listview.view = gridview. This is all done in code behind and then the user control is added to a stackpanel in code behind as well. All this is done in code behind because I'm generating different grids in the app based on conditions.
Now I have a button on the page that I want to use to pull out the data from each cell(textbox) in the tables after the user has entered in info. I created a button called Test_OnClick that calls the event method - private void Test_OnClick(object sender, RoutedEventArgs e)
What do I now need to do to get the data from the table in each usercontrol?

delete in multiple datagrids

I have three datagrids: MasterDatagrid, DetailDatagrid, AssocationDatagrid.
Bascially, if i select one row in one of the grid, i press 'delete' key to remove a row from that grid.
private void MasterDataGrid_IsMouseCapturedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
??
}
how can i replicate this function in my delete button ? how can i detect where my mouse focus is? and how to detect the right datagrid i am in?
Many thanks
One way to do it:
You can define a focus scope in the xaml on your page or user control using FocusManager.IsFocusScope.
Then on the OnClicked event of your button you can get the focused element using FocusManager.GetFocusedElement() method.
From there you can get the required item using SelectedItem(s) property and delete it.

WPF Datagrid autogenerated columns

I have bound a datatable to a datagrid in WPF. Now on clicking a row in the grid I need to have a window pop up. But for that, I need to first change a column in the datagrid to be a hyperlink. Any ideas on how to do that?
<DataGrid Name="dgStep3Details" Grid.Column="1" Margin="8,39,7,8" IsReadOnly="True" ItemsSource="{Binding Mode=OneWay, ElementName=step3Window,Path=dsDetails}" />
If I can't change an autogenerated column to hyperlink, is there a way to add a button to each row instead?
Thanks
Nikhil
So, it was really hard to create hyperlink columns to autogenerated datagrid. What I eventually did was this - create buttons to the grid on the fly and then attach a routed event for the same based on the autogenerate event of the datagrid where I shall put my code. I didn't want my code to be hardcoded to the columns and now I'm flexible by changing the datatable on the fly. Here is the code:
private void dgStep3Details_AutoGeneratedColumns(object sender, EventArgs e)
{
DataGrid grid = sender as DataGrid;
if (grid == null)
return;
DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = "More Details";
FrameworkElementFactory myButton = new FrameworkElementFactory(typeof(Button), "btnMoreDetails");
myButton.SetValue(Button.ContentProperty, "Details");
myButton.AddHandler(Button.ClickEvent, new RoutedEventHandler(btnMoreDetails_Click));
DataTemplate cellTempl = new DataTemplate();
//myButton.SetValue(Button.CommandParameterProperty, ((System.Data.DataRowView)((dgStep3Details.Items).CurrentItem)).Row.ItemArray[0]);
cellTempl.VisualTree = myButton;
col.CellTemplate = cellTempl;
dgStep3Details.Columns.Add(col);
}
public void btnMoreDetails_Click(object sender, RoutedEventArgs e)
{
//Button scrButton = e.Source as Button;
string currentDetailsKey = ((System.Data.DataRowView)(dgStep3Details.Items[dgStep3Details.SelectedIndex])).Row.ItemArray[0].ToString();
// Pass the details key to the new window
}
I don't think you'll be able to get these advanced UI features out of autogenerated columns. I think you'll either have to decide to program these columns in C# or VB.NET when you retrieve your data and tailor them the way you like, or you'll have to abandon the UI ideas you've mentioned. Autogenerated columns just cannot do that.
However, you could change your approach. Try checking into events like MouseLeftButtonDown, etc. and see if you can simulate the behavior you want by other means.

How can I put a Silverlight 3 DataGridCell into edit mode in code?

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;
}
}

Resources