Get Grid Row and Grid column of button pressed - c#

I have the following buttons:
<Button Grid.Row="0" Grid.Column="0" FontSize="48"></Button>
<Button Grid.Row="1" Grid.Column="0" FontSize="48"></Button>
<Button Grid.Row="2" Grid.Column="0" FontSize="48"></Button>
<Button Grid.Row="0" Grid.Column="1" FontSize="48"></Button>
<Button Grid.Row="0" Grid.Column="2" FontSize="48"></Button>
<Button Grid.Row="1" Grid.Column="1" FontSize="48"></Button>
<Button Grid.Row="1" Grid.Column="2" FontSize="48"></Button>
<Button Grid.Row="2" Grid.Column="1" FontSize="48"></Button>
<Button Grid.Row="2" Grid.Column="2" FontSize="48"></Button>
Whenever pressed, these buttons will call:
private void Button_Click(object sender, RoutedEventArgs e)
My question is how do I retrieve the column and row of the button that's been pressed?

From the documentation of RoutedEventHandler, which is used for the Click event:
public delegate void RoutedEventHandler(object sender, RoutedEventArgs e);
sender - The object where the event handler is attached.
Consequently, cast the sender parameter to Button and retrieve the row and column from the attached properties using its wrapper methods Grid.GetRow(...) and Grid.GetColumn(...).
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var row = Grid.GetRow(button);
var column = Grid.GetColumn(button);
// ...other code.
}

Related

Button in a different file than the main

I have 2 button but they need to be in a different file than the mainWindow.cs.
I can't figure how to do that.
So the Button_Click_2 must be in the ReadData.cs and the
Button_Click_3 must be in the WriteData.cs
The app don't recognize the button when they are not in the mainWindow.
How can I do that ?
ReadData.cs :
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
string text = verifyCard("5"); // 5 - is the block we are reading
textBlock1.Text = text.ToString();
}
MainWindow.xaml.cs :
public void Button_Click_2(object sender, RoutedEventArgs e)
{
//When I click it don't detected the code in the ReadData.cs
// The code of the button must be in the ReadData.cs
}
MainWindow.xaml :
<Grid>
<Button Content="Connexion" HorizontalAlignment="Left"
Margin="265,142,0,0" VerticalAlignment="Top" Width="236" Height="44"
Click="Button_Click_1"/>
<Button Content="Lire donnée carte" HorizontalAlignment="Left"
Margin="265,276,0,0" VerticalAlignment="Top" Width="236" Height="42"
Click="Button_Click_2"/>
<Button Content="Ecrire donnée carte" HorizontalAlignment="Left"
Margin="265,344,0,0" VerticalAlignment="Top" Width="236" Height="41"
Click="Button_Click_3"/>
<TextBox Name="textBox1" HorizontalAlignment="Left" Height="23"
Margin="321,224,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<TextBlock Name="textBlock1" HorizontalAlignment="Left"
Margin="291,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
Height="23" Width="177"/>
</Grid>
It must be possible to forced the app to detected and execute the code in another file than the Main.
I'm stuck... do any of you have the solution ?
You can't just place the button code in ReadData.cs class. as the event is related to ui of MainWindow.xaml create an object for ReadData do something like this
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
ReadData rd= new ReadData();
string text = rd.verifyCard("5");
textBlock1.Text = text.ToString();
}

WPF return value from checkbox unchecked event

How can I return a value from when check-box is unchecked? I noticed that you need to create a converter but is there an easer way of doing that?
XAML
<Grid>
<TextBox x:Name="textBox4" Visibility="Hidden" HorizontalAlignment="Left" Height="23" Margin="149,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="43" TextChanged="textBox4_TextChanged"/>
<CheckBox x:Name="checkBox" Content="Party?(4 or more)" HorizontalAlignment="Left" Margin="33,135,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Width="116" Height="23"/>
</Grid>
C#
private void checkBox_Checked(object sender, RoutedEventArgs e)
{
Handle(sender as CheckBox);
}
private void checkBox_Unchecked(object sender, RoutedEventArgs e)
{
Handle(sender as CheckBox);
}
void Handle(CheckBox checkBox)
{
bool chkd = checkBox.IsChecked.Value;
if (chkd)
{
textBox4.Visibility = Visibility.Visible;
textBox6.IsEnabled = IsEnabled.Equals(false);
}
else
{
textBox4.Visibility = Visibility.Hidden;
}
Add a handler for the Unchecked event in your XAML, and your code will get called.
Snippet:
Unchecked="checkBox_Unchecked"
Complete:
<CheckBox x:Name="checkBox" Content="Party?(4 or more)" HorizontalAlignment="Left" Margin="33,135,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Unchecked="checkBox_Unchecked" Width="116" Height="23"/>
When working with controls/events and to help discover what is available, see the Properties window and Events as in the below image:

Make click on last element

How to make click on last element?
Explanation : user click on button and move mouse in mode of click to another button. So click should be executed for last button, not for first one.
In WPF I have tried all possible events to get this (MouseUp seems to be right choice for this, but this doesn't do what I expect).
How to resolve this problem?
UPD: 2 Buttons in xaml:
<Button Grid.Column="1" Grid.Row="3" Click="ButtonThigh_OnClick" >
<Grid Width="150" Height="75">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=TrackingThighCircumference, Mode=OneWay}"/>
<TextBlock MouseRightButtonDown="Thigh_EditStarted" Height="65" Grid.RowSpan="2">
<TextBox Name="sizeThigh" Background="Transparent" Height="40" IsEnabled="False" ContextMenu="{x:Null}" ContextMenuService.IsEnabled="false" DataObject.Pasting="TextBox_Pasting" MaxLength="6" PreviewTextInput="TextBox_PreviewTextInput" LostFocus="TextBox_LostFocus" KeyUp="TextBox_KeyUp" Tag="ThighCircumferenceUI" Text="{Binding Path=ThighCircumferenceUI, Mode=TwoWay}"></TextBox>
<LineBreak/>
<TextBlock Name="ThighLb" Text="{x:Static strings:Resource.Thigh}"/><Span> (</Span><Span><TextBlock Text="{Binding ElementName=MeasurenentText, Path=Text}"/></Span><Span>)</Span>
</TextBlock>
</Grid>
</Button>
<Button Grid.Column="0" Grid.Row="4" Click="ButtonWaist_OnClick" >
<Grid Width="150" Height="75">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=TrackingJacketWaistCircumference, Mode=OneWay}"/>
<TextBlock Grid.Row="1" MouseRightButtonDown="Waist_EditStarted">
<TextBox Name="sizeWaist" Background="Transparent" Height="40" IsEnabled="False" ContextMenu="{x:Null}" ContextMenuService.IsEnabled="false" DataObject.Pasting="TextBox_Pasting" MaxLength="6" PreviewTextInput="TextBox_PreviewTextInput" LostFocus="TextBox_LostFocus" KeyUp="TextBox_KeyUp" Tag="JacketWaistCircumferenceUI" Text="{Binding Path=JacketWaistCircumferenceUI, Mode=TwoWay}"></TextBox>
<LineBreak/>
<TextBlock Name="WaistLb" Text="{x:Static strings:Resource.Waist}"/><Span> (</Span><Span><TextBlock Text="{Binding ElementName=MeasurenentText, Path=Text}"/></Span><Span>)</Span>
</TextBlock>
</Grid>
</Button>
Button Clicks :
private void ButtonThigh_OnClick(object sender, RoutedEventArgs e)
{
_resultViewModel.SelectMeasurement(si => si.ThighCircumference, vm => vm.ThighImg);
}
private void ButtonWaist_OnClick(object sender, RoutedEventArgs e)
{
_resultViewModel.SelectMeasurement(si => si.JacketWaistCircumference, vm => vm.WaistImg);
}
You can try it on buttons:
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("1");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("2");
}
private void Button1_OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
{
button1_Click(sender,e);
}
private void Button2_OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
{
button2_Click(sender,e);
}
private void Button1_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
private void Button2_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
After that, your PreviewMouseUp will work as you expect:

Get content of Current Button

<Button Name="btn1" Content="1" HorizontalAlignment="Left" Margin="13,127,0,0" VerticalAlignment="Top" Click="Button_Click"/>
<Button Name="btn2" Content="2" HorizontalAlignment="Left" Margin="135,124,0,0" VerticalAlignment="Top" Click="Button_Click"/>
How do i get the content of the current button with just one function call?
try this:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button obj = (Button)sender;
MessageBox.Show(obj.Content.ToString());
}

Why does my comboBox show all but one of its values grayed out, and doesn't allow them to be selected?

I have a ComboBox with the values 1..21
When I drop down the comboBox, number 1 (item 0) is enabled, but all the other items are grayed out, and I can't select any of them. In fact, I can only see their values when scrolling through the list - otherwise they are completely transparent.
Here is my winrt-xmal:
<ComboBox x:Name="cmbxInitialMapZoomSetting" Grid.Row="1" Grid.Column="1" Height="32" Margin="4" Width="120" HorizontalAlignment="Left" SelectionChanged="CmbxInitialMapZoomSetting_OnSelectionChanged">
<Button Content="1"/>
<Button Content="2"/>
<Button Content="3"/>
. . .
<Button Content="19"/>
<Button Content="20"/>
<Button Content="21"/>
</ComboBox>
...and here is the related code:
private void CmbxInitialMapZoomSetting_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["MapZoomSetting"] = cmbxInitialMapZoomSetting.SelectedValue;
}
Is there a reason you are putting Buttons in ComboBox? You could use ComboBoxItems instead:
<ComboBox x:Name="cmbxInitialMapZoomSetting" Height="32" Margin="4" Width="120" HorizontalAlignment="Left" SelectionChanged="CmbxInitialMapZoomSetting_OnSelectionChanged">
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
<ComboBoxItem>4</ComboBoxItem>
<ComboBoxItem>5</ComboBoxItem>
</ComboBox>
You only need a minor modification of your event handler to make this work:
private void CmbxInitialMapZoomSetting_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["MapZoomSetting"] = (cmbxInitialMapZoomSetting.SelectedValue as ComboBoxItem).Content.ToString();
}

Categories

Resources