I have 2 buttons and they do the same on different controls. How can i make this better? Because now i too much copy/paste. Here is image and code.
enter image description here
private void button_ClickOld(object sender, RoutedEventArgs e)
{
TextBoxOld.Text = SelectCatalog();
if (File.Exists(TextBoxOld + ConfigFilePath))
{
GetClientProperty(TextBoxOld.Text);
UpdateOldLabel();
}
else
{
LogsTextBox.AppendText("\nWrong folder selected - Config file doesn't exist");
}
}
private void button_ClickNew(object sender, RoutedEventArgs e)
{
TextBoxNew.Text = SelectCatalog();
if (File.Exists(TextBoxNew + ConfigFilePath))
{
GetClientProperty(TextBoxNew.Text);
UpdateNewLabel();
}
else
{
LogsTextBox.AppendText("\nWrong folder selected - Config file doesn't exist");
}
}
xaml
<Button x:Name="ButtonOld" Content="..." HorizontalAlignment="Left" Margin="149,35,0,0" VerticalAlignment="Top" Width="25" Click="button_ClickOld"/>
<TextBox x:Name="TextBoxOld" HorizontalAlignment="Left" Height="23" Margin="24,35,0,0" Text="" VerticalAlignment="Top" Width="120" IsReadOnly="True"/>
<Button x:Name="ButtonNew" Content="..." HorizontalAlignment="Left" Margin="447,35,0,0" VerticalAlignment="Top" Width="25" Click="button_ClickNew"/>
<TextBox x:Name="TextBoxNew" HorizontalAlignment="Left" Height="23" Margin="322,35,0,0" Text="" VerticalAlignment="Top" Width="120" IsReadOnly="True"/>` <Label x:Name="OldNameLabel" Content="Name" HorizontalAlignment="Left" Margin="24,70,0,0" VerticalAlignment="Top"/>
<Label x:Name="OldIpLabel" Content="IP" HorizontalAlignment="Left" Margin="24,100,0,0" VerticalAlignment="Top"/>
<Label x:Name="OldWebpageUriLabel" Content="WebpageUri" HorizontalAlignment="Left" Margin="24,130,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="OldConnectionStringTextBox" Text="ConnectionString" HorizontalAlignment="Left" Margin="24,160,0,0" Width="120"
VerticalAlignment="Top" Background="Linen" BorderThickness="0" IsReadOnly="True"/>
<Label x:Name="NewNameLabel" Content="Name" HorizontalAlignment="Left" Margin="322,70,0,0" VerticalAlignment="Top"/>
<Label x:Name="NewIpLabel" Content="IP" HorizontalAlignment="Left" Margin="322,100,0,0" VerticalAlignment="Top"/>
<Label x:Name="NewWebpageUriLabel" Content="WebpageUri" HorizontalAlignment="Left" Margin="322,130,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="NewConnectionStringTextBox" Text="ConnectionString" HorizontalAlignment="Left" Margin="322,160,0,0" Width="120"
VerticalAlignment="Top" Background="Linen" BorderThickness="0" IsReadOnly="True"/>
<Label x:Name="ArrowLabel" Content="<-" HorizontalAlignment="Left" Margin="221,14,0,0" VerticalAlignment="Top" FontSize="30" />
<Label x:Name="OldVersionTextBoxLabel" Content="Old Version:" HorizontalAlignment="Left" Margin="24,4,0,0" VerticalAlignment="Top"/>
<Label x:Name="NewVersionTextBoxLabel" Content="New Version:" HorizontalAlignment="Left" Margin="325,4,0,0" VerticalAlignment="Top"/>`
By the looks of your it, you don't understand MVVM and SOLID principles. If you truly want to take advantage of WPF, you must first learn MVVM, and to truly take advantage of MVVM, you need to understand SOLID. To reduce the burden of verbose XAML and boiler plate code, you should take advantage of MVVM frameworks, Caliburn.Micro is super easy to use but require you to have a good architecture to be able to fully utilize it.
Further, using MVVM will make your XAML code so much simpler. Old Version and New Version section will just have one UserControl and may look like
<views:VersionView DataContext={Binding OldVersionViewModel}/>
<views:VersionView DataContext={Binding NewVersionViewModel}/>
You will save yourself a lot of pain and make you delighted you chose WPF for your UI once you learn the power of MVVM.
But if this is just a small project and you insist on using code behind, you can do
private void button_ClickOld(object sender, RoutedEventArgs e)
{
SelectVerifyAndLog(TextBoxOld, UpdateOldLabel);
}
private void button_ClickNew(object sender, RoutedEventArgs e)
{
SelectVerifyAndLog(TextBoxNew, UpdateNewLabel);
}
void SelectVerifyAndLog(TextBox textBox, Action updateLabel)
{
textBox.Text = SelectCatalog();
if (File.Exists(textBox + ConfigFilePath))
{
GetClientProperty(textBox.Text);
updateLabel();
}
else
{
LogsTextBox.AppendText("\nWrong folder selected - Config file doesn't exist");
}
}
1) You can reuse your code
private void button_ClickOld(object sender, RoutedEventArgs e)
{
ButtonHelper(TextBoxOld);
}
private void button_ClickNew(object sender, RoutedEventArgs e)
{
ButtonHelper(TextBoxNew);
}
void ButtonsHelper(TextBox textBox) {
TextBoxNew.Text = SelectCatalog();
if (File.Exists(textBox + ConfigFilePath))
{
GetClientProperty(textBox.Text);
UpdateNewLabel();
}
else
{
LogsTextBox.AppendText("\nWrong folder selected - Config file doesn't exist");
}
}
2) In WPF, use MVVM, otherwise you just don't have advantages of WPF
3) If you have many groups of controls like you marked on your picture and they have much of code, make them controls. And each of them will contain it's own xaml, it's own code, and you can reuse it. It's not necessary to put all the xaml in one file.
Related
I'm very new to WPF and currently learning the concepts of data binding.
my simplified XAML code. besides my problem (below) it works fine - quick and dirty placing of objects via GUI, will be cleaned up once works:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
</Grid>
<Grid Grid.Row="1">
<GroupBox Header="Change Type:" Height="95" Width="100" VerticalAlignment="Top" Margin="270,4,422,0" >
<StackPanel>
<RadioButton x:Name="RbtAdd" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
<WrapPanel>
<TextBlock Text="Add" Foreground="Green"/>
</WrapPanel>
</RadioButton>
<RadioButton x:Name="RbtPull" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
<WrapPanel>
<TextBlock Text="Pull" Foreground="Blue"/>
</WrapPanel>
</RadioButton>
<RadioButton x:Name="RbtModify" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
<WrapPanel>
<TextBlock Text="Modify" Foreground="DarkGray"/>
</WrapPanel>
</RadioButton>
</StackPanel>
</GroupBox>
<TextBlock x:Name="txtCurStock" HorizontalAlignment="Left" Margin="330,181,0,0" TextWrapping="Wrap" Text="{Binding Path=CurrentStock}" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" TextAlignment="Center"/>
<Label Content="Current stock:" HorizontalAlignment="Left" Margin="289,156,0,0" VerticalAlignment="Top"/>
<Label x:Name ="lblOperation" Content="Stock to Pull:" HorizontalAlignment="Left" Margin="507,156,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtEntry" HorizontalAlignment="Left" Height="32" Margin="489,181,0,0" TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="120" FontSize="20" FontWeight="Bold" TextChanged="TxtEntry_TextChanged"/>
<Label Content="New Stock" HorizontalAlignment="Right" Margin="714,156,0,0" VerticalAlignment="Top" Width="68"/>
<TextBlock Text="{Binding Path=NewStock}" HorizontalAlignment="Right" Margin="0,186,10,0" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="68"/>
<TextBox x:Name="txtComment" HorizontalAlignment="Left" Height="86" Margin="289,233,0,0" TextWrapping="Wrap" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="493"/>
<Label Content="Comment:" HorizontalAlignment="Left" Margin="289,207,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name ="txtModindicator" HorizontalAlignment="Left" Margin="433,181,0,0" TextWrapping="Wrap" Text="-" FontSize="20" FontWeight="Bold" VerticalAlignment="Top"/>
<TextBlock x:Name ="txtResindicator" HorizontalAlignment="Left" Margin="663,182,0,0" TextWrapping="Wrap" Text="=" FontSize="20" FontWeight="Bold" VerticalAlignment="Top"/>
</Grid>
</Grid>
now the shortened c# code:
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace SomeWPF
{
/// <summary>
/// Interaction logic for ModifyWindow.xaml
/// </summary>
public partial class MainWindow : INotifyPropertyChanged
{
public enum Mymode
{
add,
pull,
modify
}
public Mymode mode;
public MainWindow()
{
DataContext = this;
InitializeComponent();
CurrentStock = 5;
RbtPull.IsChecked = true;
ModEntry = 1;
}
private void ModeRadio_Checked(object sender, RoutedEventArgs e)
{
if (sender != null)
{
if (sender.Equals(RbtAdd))
{
mode = Mymode.add;
txtModindicator.Text = "+";
txtComment.Text = "Add";
lblOperation.Content = "Stock to Add:";
}
else if (sender.Equals(RbtPull))
{
mode = Mymode.pull;
txtModindicator.Text = "-";
txtComment.Text = "Pull";
lblOperation.Content = "Stock to Pull:";
}
else
{
mode = Mymode.modify;
txtModindicator.Text = "~";
lblOperation.Content = "Corrected Quantity:";
txtComment.Text = "Mod";
}
TxtEntry_TextChanged(sender, null);
}
}
private void TxtEntry_TextChanged(object sender, TextChangedEventArgs e)
{
if (mode == Mymode.add)
{
NewStock = CurrentStock + ModEntry;
}
else if (mode == Mymode.pull)
{
NewStock = CurrentStock - ModEntry;
}
else
{
NewStock = ModEntry;
}
}
#region Binding Stuff
private int _newstock;
public int NewStock
{
get
{
return _newstock;
}
set
{
if (_newstock != value)
{
_newstock = value;
OnPropertyChanged();
}
}
}
private int _modentry;
public int ModEntry
{
get
{
return _modentry;
}
set
{
if (_modentry != value)
{
_modentry = value;
OnPropertyChanged();
}
}
}
private int _currentstock;
public int CurrentStock
{
get
{
return _currentstock;
}
set
{
if (_currentstock != value)
{
_currentstock = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
So this window is a popup in a little program for an inventory storage for the users to enter movements of the inventory.
everything loads fine so far and I now wanted to do the quite simple calculation part. with "old" winforms c# you'd just take the values and update the text property of the result "manually" but of course we (I) want to learn new stuff and do stuff with data binding.
The code also does the calculation, but the trigger is somehow not what I want.
let's say current stock is 5
when window loads, the mode is set to Pull (RbtPull) and the user entry (Binding to ModEntry) is set to 1 via code. The NewStock therefore should be 4 which displays correctly. (yey)
Also the comment field (for debugging for now) displays the ModEntry value 1.
so far so good.
Now I enter 3 in the Stock to Pull field, but nothing happens. (I want it to react "realtime"). The new Stock is still displayed as 4, the comment is still displayed as 1.
When I leave the field (and click into the comment field) - the property change is detected and the Comment Field shows also 3 (=ModEntry) - so it's not "realtime" but only triggers when the field is losing focus, but that would be also acceptable.
The real problem is: The new Stock stays 4 and does not calculate.
Now when I enter the Stock to Pull field again and change the value to let's say 5, the New Stock field updates to 2 (so to the value I entered before 5-3=2)
Overwriting the field with again 5 will change the new Stock to 0.
So it's always "one step behind".
From what I have found i have an idea, that I need some kind of Binding Converter instead of my method of calculating things, but I can't really find anything suitable and am not familiar enough yet with data binding. Have tried out some things already directly in the binding variable code but none worked. If anyone could hint me in the right direction I'd be very thankful. (don't need a silver plate solution but just an idea what way to search (e.g. if the sort of binding I use makes sense at all or if there's something I have to add etc.).
Thanks a lot!
PS: of course if someone is motivated to give a silver plate solution I'd also be grateful. :) - and sorry for the bad english, no native speaker.
#nosale 's second link (see comments) provided the answer to the Problem.
Setting both XAML fields txtEntry and the Result field to UpdateSourceTrigger=PropertyChanged solved the issue.
so the correct block Looks like this now without changes to the c# code:
<Label Content="Current stock:" HorizontalAlignment="Left" Margin="289,156,0,0" VerticalAlignment="Top"/>
<Label x:Name ="lblOperation" Content="Stock to Pull:" HorizontalAlignment="Left" Margin="507,156,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtEntry" HorizontalAlignment="Left" Height="32" Margin="489,181,0,0" TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Path=ModEntry, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120" FontSize="20" FontWeight="Bold" TextChanged="TxtEntry_TextChanged"/>
<Label Content="New Stock" HorizontalAlignment="Right" Margin="714,156,0,0" VerticalAlignment="Top" Width="68"/>
<TextBlock Text="{Binding Path=NewStock, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Right" Margin="0,186,10,0" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="68"/>
<TextBox x:Name="txtComment" HorizontalAlignment="Left" Height="86" Margin="289,233,0,0" TextWrapping="Wrap" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="493"/>
<Label Content="Comment:" HorizontalAlignment="Left" Margin="289,207,0,0" VerticalAlignment="Top"/>
Reason is, that textboxes have a Default UpdateSourceTrigger=LostFocus and not PropertyChanged to prevent updates with user having entered typos.
something new learned: WPF is cool and automatically handles non plausible values like null or strings and marks the field red! :)
thanks again for the links!
I have the below code but it is only happening once on first hold and not after.
Properly something is very simple but it can't seem to find a answer.
private void Contact_Tap_1_Holding(object sender, RightTappedRoutedEventArgs e)
{
contactSelected = 1;
this.Frame.Navigate(typeof(ContactAddPopUp));
}
<Button x:Name="Contact_1" HorizontalAlignment="Stretch" Margin="40,464,51,0"
RightTapped="Contact_Tap_1_Holding" VerticalAlignment="Top" Height="72"
Background="#ff4081" FontSize="11" FontWeight="Bold" Foreground="#FFFFFF"
Click="button_Click" Content="Contact 1" />
Thanks!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
(I've read similar posts but they all had a twist to them that made the solution different)
I'm porting a WinForms app that used this:
myComboBox.SetSelected(myComboBox.FindString("Some Text"), true);
to select an item programmatically. When porting over to WPF, I tried this but it has no effect (the item does not get selected):
myComboBox.SelectedItem = myComboBox.FindName("Some Text");
What is the correct way to select an existing item in a ComboBox, in WPF?
You have to use SelectedValue. In WPF ComboBox, there are multiple ways to achieve the same thing. So, one syntax to select an item programmatically won't work. There are various ways of adding items to ComboBox.
You can set ItemsSource both declaratively or in code.
You can add ComboBoxItems etc. See Items property in property window to see various item-types available.
If you are using ItemsSource with string values, then you need syntax like : cmb1.SelectedValue = "Name1"
If you are directly adding items like <ComboBox ...> <ComboBoxItem Content="Name1"/> </ComboBox/> , then you need
foreach (ComboBoxItem item in cmb2.Items)
if (item.Content.ToString() == "Name1")
{
cmb2.SelectedValue = item;
break;
}
I have posted a full working sample demonstrating how to select an item programmatically in various scenarios. Sample code (can be used as is) :
Pay attention to last one, where you have to use SelectedValuePath.
Window1.xaml
<Window x:Class="WpfApplicationBlend.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="411" Width="749">
<Grid>
<Grid Margin="30,27,491,276">
<ComboBox x:Name="cmb1" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBox.ItemsSource>
<CompositeCollection>
<sys:String>Name1</sys:String>
<sys:String>Name2</sys:String>
<sys:String>Name3</sys:String>
<sys:String>Name4</sys:String>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
<TextBox x:Name="tbInput1" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/>
</Grid>
<Grid Margin="405,27,111,276">
<ComboBox x:Name="cmb2" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBoxItem Content="Name1"/>
<ComboBoxItem Content="Name2"/>
<ComboBoxItem Content="Name3"/>
</ComboBox>
<TextBox x:Name="tbInput2" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button2_Click"/>
</Grid>
<Grid Margin="30,207,491,96">
<ComboBox x:Name="cmb3" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBox.ItemsSource>
<CompositeCollection>
<sys:String>Name1</sys:String>
<sys:Boolean>True</sys:Boolean>
<sys:Int32>123</sys:Int32>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
<TextBox x:Name="tbInput3" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button3_Click"/>
</Grid>
<Grid Margin="405,207,116,96">
<ComboBox x:Name="cmb4" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" SelectedValuePath="Name" DisplayMemberPath="Name">
</ComboBox>
<TextBox x:Name="tbInput4" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button4_Click"/>
</Grid>
</Grid>
</Window>
Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections;
namespace WpfApplicationBlend
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<Employee> employees = new List<Employee>()
{
new Employee(){Name="Name1", Age=100},
new Employee(){Name="Name2", Age=101},
};
cmb4.ItemsSource = employees;
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
cmb1.SelectedValue = tbInput1.Text;
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
foreach (ComboBoxItem item in cmb2.Items)
if (item.Content.ToString() == tbInput2.Text)
{
cmb2.SelectedValue = item;
break;
}
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
foreach (object item in cmb3.Items)
if (item.ToString() == tbInput3.Text)
{
cmb3.SelectedValue = item;
break;
}
}
private void Button4_Click(object sender, RoutedEventArgs e)
{
cmb4.SelectedValue = tbInput4.Text;
}
}
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
}
}
comboboxName.SelectedIndex = yourIndex;
e.g.
combobox1.SelectedIndex = 2;
Is there a way to pass arguments on button click in Windows Phone 8.1?
I have a grid of 5x5 buttons, and they should all call the same method but with a different parameter. I am adding a handler like this:
foreach (var child in buttonGrid.Children)
{
Button b = child as Button;
if (b != null)
{
b.Click += Button_Click;
// I want to add an argument to this
}
}
Now the only way I can get the index of the button is by iterating over the whole grid and checking if the sender is equal to the button:
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < buttonGrid.Children.Count; i++)
{
if (sender == buttonGrid.Children[i])
{
DoSomething(i);
return;
}
}
}
It works, but I don't really like this approach. Is there a more efficient way of doing this (other than creating a different method for each of the 25 buttons)?
I tried searching on the internet, but the documentation and examples for Windows Phone are really lacking. If anyone has a good repository of Windows Phone 8.1 tutorials to direct me to, that would also be of help.
You can use Tag property of the button.
For eg.
I'm trying to create a number pad which has 9 buttons with the respective number as the button content and i have set the same thing as the Tag property also.
<StackPanel>
<StackPanel Orientation="Horizontal" >
<Button Content="1" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="1" />
<Button Content="2" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="2" />
<Button Content="3" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="3" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="4" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="4" />
<Button Content="5" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="5" />
<Button Content="6" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="6" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="7" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="7" />
<Button Content="8" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="8" />
<Button Content="9" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="9" />
</StackPanel>
</StackPanel>
This produces the following output :
In your code behind you can now use the Tag property in the following manner
private void Button_Click(object sender, RoutedEventArgs e)
{
var tag = (sender as Button).Tag;
int t = Convert.ToInt16(tag);
switch (t)
{
case 1:
//Do Something
break;
case 2:
//Do Something
break;
case 3:
//Do Something
break;
case 4:
//Do Something
break;
case 5:
//Do Something
break;
case 6:
//Do Something
break;
case 7:
//Do Something
break;
case 8:
//Do Something
break;
case 9:
//Do Something
break;
default:
break;
}
}
Controls and other elements in XAML have a Tag property that you can set to an arbitrary object value for this kind of thing. Set it when you create the object and then inspect it in the event handler.
A simple solution would be to assign unique numbers to each button and then call the function with this uniquely assigned number as a parameter.
In the function, you could easily use the If-Else blocks to perform the task according to the Unique Number.
The code for 4 buttons becomes something like:-
void func(int unique_number)
{
if(unique_number==1)
{
//perform tasks for button 1
}
if(unique_number==2)
{
//perform tasks for button 2
}
if(unique_number==3)
{
//perform tasks for button 3
}
if(unique_number==4)
{
//perform tasks for button 4
}
}
private void Button_1_Click(object sender, RoutedEventArgs e)
{
func(1); //Call from button 1
}
private void Button_2_Click(object sender, RoutedEventArgs e)
{
func(2); //Call from button 2
}
private void Button_3_Click(object sender, RoutedEventArgs e)
{
func(3); //Call from button 3
}
private void Button_4_Click(object sender, RoutedEventArgs e)
{
func(4); //Call from button 4
}
I hope this makes it somewhat easy and efficient.
While the answers show you how to do this using Tag, this approach is non-convenient for, say 100 buttons.
To achieve your result, you can use closures for the job. Here's how:
// When you're attaching handlers
int i=0;
foreach (var child in buttonGrid.Children)
{
Button b = child as Button;
if (b != null)
{
b.Click += () => {
int z = i++; // You can put a different id generator here as well, like int z = <rand>%<prime> if you want
DoSomething(z); // DoSomething called with corrosponding button id
}
}
}
This approach does not require you to use tags!
I don't know how to get text from "firstBox" and "secondBox" after button click.
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<!-- some code -->
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Data}" VerticalAlignment="Top" Height="18" Width="100" FontSize="13.333" Margin="162,9,0,0"/>
<TextBlock HorizontalAlignment="Left" Margin="0,35,0,0" TextWrapping="Wrap" Text="{Binding D_gospodarzy}" FontSize="14.667" VerticalAlignment="Top" Height="59" Width="100"/>
<TextBlock HorizontalAlignment="Center" Margin="268,35,7,0" TextWrapping="Wrap" Text="{Binding D_gosci}" FontSize="14.667" VerticalAlignment="Top" Width="100" Height="59"/>
<TextBox x:Name="firstBox" ... />
<Button Content="Click" " Click="Button_Click_1"/>
<TextBox x:Name="secondBox" ... />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
I get only the object
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var myobject = (sender as Button).DataContext;
}
There are cuple of ways to do it, for example you can traverse the VisualTree of clicked button's parent and retrive TextBox with the name you want. In this case, I would take advantage of an extension method written by yasen in this answer.
Then it can look for example like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var parent = (sender as Button).Parent;
TextBox firstOne = parent.GetChildrenOfType<TextBox>().First(x => x.Name == "firstBox");
Debug.WriteLine(firstOne.Text);
}
Remember to put an extension method somewhere in a static class:
public static class Extensions
{
public static IEnumerable<T> GetChildrenOfType<T>(this DependencyObject start) where T : class
{
// rest of the code
Here's how to get the text..
String text1 = firstBox.Text;
String text2 = secondBox.Text;
note: firstBox and secondBox must be class members to use them in different class methods.