Reputation: 25058
Having a user control template SubCtrl
that gets an id on constructor to
set a label CounterLabelText
and consists on:
1 checkbox
1 button
1 RadNumeric
<Grid Height="27" Width="602">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="533*" />
<ColumnDefinition Width="44*" />
</Grid.ColumnDefinitions>
<Label Margin="26,0,492,0" Content="{Binding CounterLabelText}" />
<CheckBox Height="16" HorizontalAlignment="Left" Margin="7,5,0,0" Name="ChkBxOrder" Width="21" VerticalAlignment="Top" telerik:StyleManager.Theme="Vista" IsManipulationEnabled="True" />
<!--<telerik:RadButton Height="22" HorizontalAlignment="Left" Margin="343,1,0,0" Name="ButtonDoSomething" Width="58" VerticalAlignment="Top" telerik:StyleManager.Theme="Vista" Content="Set Points" Click="ButtonDoSomething_Click" />-->
<telerik:RadNumericUpDown Height="18" HorizontalAlignment="Left" Margin="412,1,0,0" Name="radx" Width="40" VerticalAlignment="Top" IsInteger="True" telerik:StyleManager.Theme="Vista"/>
</Grid>
public partial class SubCtrl : UserControl
{
private string _counterLabelText;
public string CounterLabelText
{
get
{
return _counterLabelText;
}
set
{
_counterLabelText = value;
OnPropertyChanged("CounterLabelText");
}
}
public SubCtrl(int id)
{
InitializeComponent();
CounterLabelText = id.ToString();
DataContext = this;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
As user control is created dynamically on ADD button
inside main form passing a counter like
public partial class MainCtrl : UserControl
{
int templateRowsCounter;
//constructor and variables
//...
//...
private void AddTemplate_Click(object sender, RoutedEventArgs e)
{
StackRefinement.Children.Add(new SubCtrl (++templateRowsCounter));
}
private void DelTemplate_Click(object sender, RoutedEventArgs e)
{
//how to remove it based on templateRowsCounter ???
}
}
I have two questions
SubCtrl
with a checkbox, a button and a radNumeric
How can I know when ButtonDoSomething_Click() method must be called
when pressing ButtonDoSomething
, and also how to get state of checkbox and RadNumeric, I mean How to detect the events?Upvotes: 0
Views: 121
Reputation: 13679
Update
SubCtrl class
additionally I would prefer to use DependencyProperty instead of INotifyPropertyChanged when I am having UserControl as base class so modified CounterLabelText too
public partial class SubCtrl : UserControl
{
public event EventHandler ButtonDoSomethingClicked;
public string CounterLabelText
{
get { return (string)GetValue(CounterLabelTextProperty); }
set { SetValue(CounterLabelTextProperty, value); }
}
// Using a DependencyProperty as the backing store for CounterLabelText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CounterLabelTextProperty =
DependencyProperty.Register("CounterLabelText", typeof(string), typeof(SubCtrl), new PropertyMetadata(null));
public SubCtrl(int id)
{
InitializeComponent();
CounterLabelText = id.ToString();
DataContext = this;
ButtonDoSomething.Click += ButtonDoSomething_Click;
}
void ButtonDoSomething_Click(object sender, RoutedEventArgs e)
{
ButtonDoSomethingClicked.Invoke(this, EventArgs.Empty);
}
}
code for the MainCtrl
class code
public partial class MainCtrl : UserControl
{
int templateRowsCounter;
private void AddTemplate_Click(object sender, RoutedEventArgs e)
{
SubCtrl sub = new SubCtrl(++templateRowsCounter);
sub.ButtonDoSomethingClicked += sub_ButtonDoSomethingClicked;
StackRefinement.Children.Add(sub);
}
private void DelTemplate_Click(object sender, RoutedEventArgs e)
{
SubCtrl sub = (SubCtrl)StackRefinement.Children[--templateRowsCounter];
sub.ButtonDoSomethingClicked -= sub_ButtonDoSomethingClicked;
StackRefinement.Children.Remove(sub);
}
void sub_ButtonDoSomethingClicked(object sender, EventArgs e)
{
//your do something logic
}
}
so this sample propogates up the ButtonDoSomethingClicked, rest can also be implemented in same way. ButtonBase.Click is also useful when you can not modify the code, also it is bit more generic approach.
previous
here is your quick solution for question 1
private void DelTemplate_Click(object sender, RoutedEventArgs e)
{
//remove last added template, advisable to check if exists before deleting it
StackRefinement.Children.RemoveAt(StackRefinement.Children.Count - 1);
}
Upvotes: 1