Pass data by CommandParameter - c#

I am trying to pass UserName by CommandParameter.
<GridViewColumn Width="Auto" Header="UserName" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Click="Button_Click" Content="..." CommandParameter="{Binding Path=UserName}"></Button> </DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
But here I am not sure what do I have to use to catch it...
private void Button_Click(object sender, RoutedEventArgs e)
{
// How to get UserName here?
}
Any clue?
Thank you!

Best you can do with such approach is this:
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = (Button) sender;
var userName = button.CommandParameter;
}
Though it will do the job but it is not how CommandParameter is supposed to be used. CommandParameter is supposed to be passed as parameter to button's command. So if you will use Command instead of handling Button.Click your command will receive your data in its Execute and CanExecute handlers. See samples for commands&buttons in google. Here is first link from google for example: How to use Commands in WPF

If you are going to use the click handler, why not bind the Button's Tag property to UserName? A little more along the lines of what the Tag property was intended than a misuse of Commanding principals.
<Button Click="Button_Click" Content="Button Content" Tag="{Binding UserName}"/>
private void Button_Click(object Sender, RoutedEventArgs e)
{
var Button = (Button)Sender;
var UserName = (string)Button.Tag;
}

Related

C# / WPF Unmask password inside the passwordBox

How could I unmasked and masked the password inside the passwordBox whenever I click the checkBox? I'm using C# WPF template.
Here is my .XAML code:
<PasswordBox x:Name="passwordBox_password" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="5" Height="25" />
<CheckBox x:Name="checkBox_showPassword" Grid.Row="3" Grid.Column="1" Margin="5,0,5,5" Content="show password" Checked="checkBox_showPassword_Checked" Unchecked="checkBox_showPassword_Unchecked" />
Here is my .CS code:
private void checkBox_showPassword_Checked(object sender, RoutedEventArgs e)
{
// what to do here ?
}
private void checkBox_showPassword_Unchecked(object sender, RoutedEventArgs e)
{
// what to do here ?
}
Or is there another way to do it in WPF?
It's very simple to do that.
First you should to add the value PasswordChar in your PasswordBox:
<PasswordBox Name="PasswordHidden" PasswordChar="•"/>
Next under the PasswordBox tag you should to add a TextBox with Visibility value setted to Hidden:
<TextBox Name="PasswordUnmask" Visibility="Hidden"/>
And a trigger to show / hide the password, for example a simple text or a button. In my case I'm using a simple text.
<TextBlock Name="ShowPassword"/>
Next you need to add 3 different events in the trigger element, for example (this is valid for TextBlock or Image, if you want to use a Button you should to choose another events):
<TextBlock x:Name="ShowPassword" Text="SHOW" PreviewMouseDown="ShowPassword_PreviewMouseDown" PreviewMouseUp="ShowPassword_PreviewMouseUp" MouseLeave="ShowPassword_MouseLeave"/>
The events are PreviewMouseDown PreviewMouseUp and MouseLeave but you can choose the appropriate event for your situation.
Now in your code you need to program the functions:
private void ShowPassword_PreviewMouseDown(object sender, MouseButtonEventArgs e) => ShowPasswordFunction();
private void ShowPassword_PreviewMouseUp(object sender, MouseButtonEventArgs e) => HidePasswordFunction();
private void ShowPassword_MouseLeave(object sender, MouseEventArgs e) => HidePasswordFunction();
private void ShowPasswordFunction()
{
ShowPassword.Text = "HIDE";
PasswordUnmask.Visibility = Visibility.Visible;
PasswordHidden.Visibility = Visibility.Hidden;
PasswordUnmask.Text = PasswordHidden.Password;
}
private void HidePasswordFunction()
{
ShowPassword.Text = "SHOW";
PasswordUnmask.Visibility = Visibility.Hidden;
PasswordHidden.Visibility = Visibility.Visible;
}
The following link will bring you to the answer you are looking for my good sir. Mr Lamas did a great job of answering the how-to so I'd rather redirect you to the answer :)
showing password characters on some event for passwordbox
I recommend Using MahApps.Metro ... after installing it from nuget.org ... you must use it in the head of your xaml like this
xmlns:controls="http://metro.mahapps.com/winf/xaml/controls"
and then ... just use it's style for your PasswordBox control
<PasswordBox Style="{StaticResource MetroButtonRevealedPasswordBox}" />
you can even change the content for the show icon using the controls:PasswordBoxHelper.RevealButtonContent attached property

Returning/Getting DataGrid Row index from button click

I'm having trouble returning which row a button was clicked from in my wpf project.
I have the following so far...
XAML -
<DataGrid x:Name="myDataGrid" HorizontalAlignment="Left" Width="550">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"></DataGridTextColumn>
<DataGridTemplateColumn Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="RowEvent">X</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
C# -
private void RowEvent(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
}
I seem to recall that when I was using a dataGridView in Win Forms I could get the row index from e.RowIndex if I remember correctly, but I cant seem to do that this time.
Could anyone point me in the right direction?
Just use:
DataGrid.SelectedIndex Property
... like this:
private void RowEvent(object sender, RoutedEventArgs e)
{
int index = myDataGrid.SelectedIndex;
MessageBox.Show(index.ToString());
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
myDataGrid.GetColumn(btn);
myDataGrid.GetRow(btn);
MessageBox.Show(myDataGrid.GetColumn(btn).ToString());
MessageBox.Show(myDataGrid.GetRow(btn).ToString());
}
This is how i managed it! I just used the click event of the button

Hyperlinks in Xaml to navigate from one page to another

How can I create Hyperlinks in Xaml to navigate from one page to another page? I don't know actually how to use the hyperlink tags.
You can use RequestNavigate event to add a HyperLink class
Xaml:
<TextBlock>
<Hyperlink NavigateUri="http://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">
Click here
</Hyperlink>
</TextBlock>
Code Behind:
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
If you are looking for navigating to another page :
<StackPanel Grid.Row="1"
Margin="120,0,120,60">
<HyperlinkButton Content="Click to go to page 2" Click="HyperlinkButton_Click"/>
</StackPanel>
And handle it like :
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(BasicPage2));
}
And to move to external page : As mentioned by #Brainy

How to get DataGrid colum value when a button is clicked from inside DataGrid?

I Have a silverlight DataGrid like below
<sdk:DataGrid Name="DataGridDream" AutoGenerateColumns="False" HeadersVisibility="None" Background="Yellow" HorizontalAlignment="Center" Height="100" VerticalAlignment="Center" Width="120" >
<sdk:DataGrid.Columns >
<sdk:DataGridTextColumn Binding="{Binding By}" >
</sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Binding="{Binding UserName}" >
</sdk:DataGridTextColumn>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="ButtonSave" Content="Take" Click="ButtonSave_Click">
</Button>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
and i have a Click event of the button inside DataGrid,
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
int Value = // Value of the UserId column when a button is clicked on that row
}
how to do that ?
This will get you value of UserName property of clicked row:
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
string userName = (((Button)sender).DataContext as ModelClass).UserName;
}
ModelClass is where your property resides i.e. underlying source class for your bindings.
You can get the selected item from the DataGrid try something like this:
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
var row = (int)DataGridDream.SelectedItem;
}
You can obtain the current row values as a dynamic object. You can access all the values in the grid row using that object.
The modified version of Mike Schwartz's solution would be
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
dynamic row = DataGridDream.SelectedItem;
}
You can also obtain the row values as Rohit Vats suggested. But by using the dynamic keyword you will not have to declare a Model class.
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
dynamic row = ((Button)sender).DataContext;
}
After that, you can access the UserName like this
var UserName = row.UserName;

How to select an item from a listbox in windows phone?

I've a list of data,
Each row will show a data and will have a button, when i click the data shown i want give some data to the previous page and when i click the button in the same row i want to send that same data to next page.
My Xaml code,
<ListBox x:Name="List" HorizontalAlignment="Left" Height="612" Margin="6,7,0,0" VerticalAlignment="Top" Width="443" SelectionChanged="List_SelectionChanged_1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="420" Height="50">
<TextBlock x:Name="tbName" Width="400" Height="44" FontSize="22" FontWeight="Bold" Text="{Binding Name}" />
<Button x:Name="DetailButton" Height="44" Width="20" Content=">" FontWeight="Bold" Click="DetailButton_Click_1"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and the code for List_SelectionChanged_1 event handler is,
private void List_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
Display selectedItemData = (sender as ListBox).SelectedValue as Display;
NavigationService.Navigate("/Page1.xaml",selectedItemData);
}
and my DetailButton_Click_1 event handler is,
private void DetailButton_Click_1(object sender, RoutedEventArgs e)
{
Display selectedItemData = (sender as ListBox).SelectedValue as Display;
NavigationService.Navigate("/page3.xaml", selectedItemData);
}
Things work fine for *List_SelectionChanged_1*, but i get an exception while executing
Display selectedItemData = (sender as ListBox).SelectedValue as Display;
of the DetailButton_Click_1 , i get an exception a null exception,
An exception of type 'System.NullReferenceException' occurred in ExpenseApp.DLL but was not handled in user code
What should i do make it work?
The underlying problem is that the sender of the button click event is the button, not the ListBox.
Also note that clicking the button on your data template will not necessarily select that item in the list. Try to grab the clicked item's data context and use that instead of .SelectedItem
private void DetailButton_Click_1(object sender, RoutedEventArgs e)
{
var clickedUIElement = sender as Button;
if (null == clickedUIElement) { Return; }
Display selectedItemData = clickedUIElement.DataContext as Display;
if(null != selectedItemData)
{
NavigationService.Navigate("/page3.xaml", selectedItemData);
}
}
Your code, as it stands, will have a null reference since you can't cast a Button as a ListBox.
try verify if the selectvalue is null before execute the code:
private void DetailButton_Click_1(object sender, RoutedEventArgs e)
{
If ((sender as ListBox).SelectedValue != null){
Display selectedItemData = (sender as ListBox).SelectedValue as Display;
NavigationService.Navigate("/page3.xaml", selectedItemData);
}
}

Categories

Resources