I create a stackpanel, and put some controls and 1 of these is a textbox, programaticly.
How can i create an event that, when i click on textbox i delete the stackpanel and everything inside?
Here is my code...
System.Windows.Controls.StackPanel Defesa = new StackPanel();
Defesa.Height = 120;
Defesa.Width = 140;
Panel_DE.Children.Add(Defesa);
Defesa.Background = new
SolidColorBrush((Color)ColorConverter.
ConvertFromString("#FF000000"));
Defesa.Margin = new Thickness(20, 0, 0, 0);
Ellipse Foto = new Ellipse();
Foto.Height = 80;
Foto.Width = 80;
Foto.StrokeThickness = 2;
SolidColorBrush borda = new SolidColorBrush();
borda.Color = Colors.White; ;
Foto.Stroke = borda;
Defesa.Children.Add(Foto);
ImageBrush camera = new ImageBrush();
camera.ImageSource = new BitmapImage(new
Uri(#"C:\Users\Vitor\documents\visual studio 2013\Projects\Manager
Treinador\Manager Treinador\Icons\photo-camera w.png"));
Foto.Fill = camera;
System.Windows.Controls.TextBlock Nome_def = new TextBlock();
Nome_def.Text = def_name;
Nome_def.Foreground = new
SolidColorBrush((Color)ColorConverter.ConvertFromString
("#FFF9F4F4"));
Nome_def.HorizontalAlignment = HorizontalAlignment.Center;
Defesa.Children.Add(Nome_def);
System.Windows.Controls.StackPanel Panel = new StackPanel();
Panel.Orientation = Orientation.Horizontal;
Defesa.Children.Add(Panel);
System.Windows.Controls.TextBlock But_Delet = new TextBlock();
But_Delet.Text = "X";
But_Delet.FontWeight = FontWeights.Bold;
But_Delet.Foreground = new
SolidColorBrush((Color)ColorConverter.ConvertFromString
("#FFF90707"));
Panel.Children.Add(But_Delet);
But_Delet.HorizontalAlignment = HorizontalAlignment.Left;
But_Delet.Cursor = Cursors.Hand;
But_Delete is the text box that i want to remove all...
Thanks
Since a TextBlock does not have a click event, you will need to simulate this by using OnPreviewMouseDown event. To do this:
add this to the end of your code:
But_Delet.PreviewMouseDown += ButDeletOnPreviewMouseDown;
Then add this event handler:
private void ButDeletOnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Panel_DE.Children.Remove(Defesa);
}
However, if there is a chance you will need to restore these controls, it would better to hide them using this event handler instead:
private void ButDeletOnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Defesa.Visibility = Visibility.Hidden;
}
I have a basic combobox in a form. Compared to other controls(Button,label, etc) the height of the combobox doesn't change when the resolution is changed.
public partial class Form1 : Form
{
string result;
string fontInformation;
private bool scaleFactorKnown = false;
private SizeF scaleFactor;
public Form1()
{
SizeChanged += Form1_SizeChanged;
InitializeComponent();
label1.Location = new Point(12, 36);
label1.Size = new Size(100, 21);
label1.Scale(scaleFactor);
//
// textBox1
//
textBox1.Location = new Point(133, 33);
textBox1.Size = new Size(100, 21);
textBox1.Scale(scaleFactor);
//
// comboBox1
//
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
// button1
//
button1.Location = new Point(365, 32);
button1.Size = new Size(100, 21);
button1.Scale(scaleFactor);
//
// radioButton1
//
radioButton1.Location = new Point(480, 32);
radioButton1.Size = new Size(100, 21);
radioButton1.Scale(scaleFactor);
//
// checkBox1
//
checkBox1.Location = new Point(586, 33);
checkBox1.Size = new Size(100, 21);
checkBox1.Scale(scaleFactor);
//
// textBox2
//
textBox2.Location = new Point(26, 102);
textBox2.Size = new Size(660, 250);
textBox2.Scale(scaleFactor);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (!scaleFactorKnown)
{
scaleFactor = AutoScaleFactor;
scaleFactorKnown = true;
}
Size controlSize = new Size((int)(comboBox1.Width * scaleFactor.Width),
(int)(comboBox1.Height * scaleFactor.Height)); //use for sizing
//set bounds
comboBox1.Bounds = new Rectangle(comboBox1.Location, controlSize);
}
}
I have tried the method Scale() to scale all other controls, it works for other controls except for combobox. I also tried manually changing the bound but it didn't work. I also tried change the anchor and dock as well.
Expected result: Combobox height(At 150%)=42
Actual result: Combobox
height(At 150%)=28
Would appreciate any help on how to fix this issue.
You have to set the IntegralHeight property of the ComboBox to false:
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
comboBox1.IntegralHeight = false;
I am trying to pass parameters from a page to another page. These passed parameters will be used to select from an SQL table.
The page is built as follows: (The code behind)
private MainRoutePageViewModel mainroutepageviewmodel;
private List<RouteInfo> routeinfo;
Constructor:
public MainRoutePageViewDetail(MessagDatabase database)
{
InitializeComponent();
BindingContext = mainroutepageviewmodel = new MainRoutePageViewModel(database,Navigation);
//_listOfProperties = mainroutepageviewmodel.GetLabelInfo();
ScrollView scrollview = new ScrollView();
StackLayout mainstack = new StackLayout();
mainstack.Spacing = 0;
mainstack.Padding = 0;
//mainstack.HeightRequest = 2000;
routeinfo = mainroutepageviewmodel.GetLabelInfo();
string _routePlacer = "";
foreach (var i in routeinfo)
{
mainstack.Children.Add(NewRouteName(i.RouteName));
mainstack.Children.Add(BuildNewRoute(i.RouteStops,i));
_routePlacer = i.RouteName;
}
scrollview.Content = mainstack;
Content = scrollview;
}// end of constructor
The BuildNewRoute method:
public StackLayout BuildNewRoute(List<string> location, RouteInfo routeinfo)
{
StackLayout stackLayout = new StackLayout();
//stackLayout.HeightRequest = 1000;
foreach (var i in location) {
StackLayout stackLayout2 = new StackLayout();
stackLayout2.HeightRequest = 200;
Grid grid = new Grid();
grid.ColumnSpacing = 0;
grid.RowSpacing = 0;
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(15, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(55, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(40, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(10, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(15, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Star) });
TapGestureRecognizer ArrowtapGesture = new TapGestureRecognizer();
ArrowtapGesture.Tapped += ArrowtapGesture_Tapped;
// Arrow icon
Image arrowimage = new Image();
arrowimage.Source = "Resources/arrow.png";
arrowimage.VerticalOptions = LayoutOptions.Center;
arrowimage.HorizontalOptions = LayoutOptions.Center;
arrowimage.GestureRecognizers.Add(ArrowtapGesture);
grid.Children.Add(arrowimage,7,6);
// total weight labels
Label weightlabel = new Label();
weightlabel.Text = "Total Weight [kg]: ";
grid.Children.Add(weightlabel,1,5,3,4);
// total items labels
Label itemsLabel = new Label();
itemsLabel.Text = "Total Items: ";
grid.Children.Add(itemsLabel, 1, 5, 4, 5);
// underline labels
Label firstunderline = new Label();
Label secondunderline = new Label();
firstunderline.BackgroundColor = Color.Black;
secondunderline.BackgroundColor = Color.Black;
grid.Children.Add(firstunderline,0,9,0,1);
grid.Children.Add(secondunderline,0,9,2,3);
// address label
Label labelLocation = new Label();
labelLocation.Text = i;
grid.Children.Add(labelLocation, 0, 3);
//sequence label
Label sequencelable = new Label();
sequencelable.Text = "Sequence: ";
sequencelable.VerticalTextAlignment = TextAlignment.Center;
grid.Children.Add(sequencelable, 0, 1);
// slot label
Label slotlabel = new Label();
slotlabel.Text = "ETA/Slot: ";
slotlabel.VerticalTextAlignment = TextAlignment.Center;
grid.Children.Add(slotlabel,1,4,1,2);
// time label
Label timelabel = new Label();
timelabel.Text = "Time: ";
timelabel.VerticalTextAlignment = TextAlignment.Center;
grid.Children.Add(timelabel, 4, 5,1,2);
// Status label
Label statuslabel = new Label();
statuslabel.Text = "Status: ";
statuslabel.VerticalTextAlignment = TextAlignment.Center;
grid.Children.Add(statuslabel, 5, 6,1,2);
//start button
Button startbutton = new Button();
startbutton.Text = "Pending";
startbutton.BackgroundColor = Color.Gray;
grid.Children.Add(startbutton,5,8,4,6);
// Phone book image
Image bookImage = new Image();
//bookImage.BackgroundColor = Color.White;
bookImage.Source = "Resources/phoneWithBook.png";
bookImage.VerticalOptions = LayoutOptions.Center;
bookImage.HorizontalOptions = LayoutOptions.Center;
grid.Children.Add(bookImage,1,2,6,7);
//Globe image
Image GlobeImage = new Image();
// GlobeImage.BackgroundColor = Color.White;
GlobeImage.Source = "Resources/globe.png";
GlobeImage.VerticalOptions = LayoutOptions.Center;
GlobeImage.HorizontalOptions = LayoutOptions.Center;
grid.Children.Add(GlobeImage, 2, 3, 6, 7);
stackLayout2.Children.Add(grid);
stackLayout.Children.Add(stackLayout2);
}
return stackLayout;
}
As you can probably see it loops through a list of collected data and adds grids and labels to a main StackLayout.
This is not the issue the page building works fine.
What you can see is the arrow icon image that has a tap gesture attached to it. This tap gesture uses the view model to open the next page.
The tap gesture:
private async void ArrowtapGesture_Tapped(object sender, EventArgs e)
{
await mainroutepageviewmodel.OpenStopDetail();
}
And the OpenStopDetail method:
public async Task OpenStopDetail()
{
await Navigation.PushAsync(new StopDetailPageView());
}
I want to know how to pass parameters from the tap event through to the StopDetailView page.
Specifically the text from the sequence label.
Some things I have tried, have been using the casting in the tap event but this seems to be bound to the item that is selected. In other words its giving me access to image properties. Which is no good for my situation.
I cant seem to find a way to access each label property individually to pass as a parameter. Sorry if this isn't clear, it was tough to explain. Let me know if more detail is needed.
You should be able to use the CommandParameter of the TapGestureRecognizer.
In XAML:-
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"
CommandParameter="Value"/>
e.Parameter will be whatever you set in the CommandParameter.
private async void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
Edit:
It has been pointed out that the above is not the right signature, the param, needs to be EventArgs and cast to TappedEventArgs.
private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
var param = ((TappedEventArgs)e).Parameter;
}
The sender of the Tapped event will be the control the gesture recognizer is attached to - in your case, an Image. So you can add your data to one of the Image's properties in order to access it from your event handler.
// assign parameter to ClassId (must be a string)
arrowimage.ClassId = "blah";
arrowimage.GestureRecognizers.Add(ArrowtapGesture);
private async void ArrowtapGesture_Tapped(object sender, EventArgs e)
{
// retrieve parameter from sender's ClassId
var parm = ((Image)sender).ClassId;
await mainroutepageviewmodel.OpenStopDetail();
}
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="Share_Tapped" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>
enter code here
private void Share_Tapped(object sender, TappedEventArgs e)
{
var contact = (e.Parameter) as DetailList;
}
Result
I have a pointing to null error but i dont see where the problem is since everything gets initalised before used. the points where the error acures i have given a blockquote. Like always im thankfull for every help.
Button topAddBut = null;
//Button botAddBut = null;
Button loadBut = null;
Button saveBut = null;
StackPanel topSP = null;
//StackPanel botSP = null;
public MainWindow()
{
InitializeComponent();
loadBut = new Button { Content = "Load", Width = 70, Height = 23 };
Canvas.SetRight(loadBut, 160);
Canvas.SetBottom(loadBut, 24);
canvas1.Children.Add(loadBut);
saveBut = new Button { Content = "Save", Width = 70, Height = 23 };
Canvas.SetRight(saveBut, 80);
Canvas.SetBottom(saveBut, 24);
canvas1.Children.Add(saveBut);
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
Canvas.SetLeft(topSP, 160);
Canvas.SetTop(topSP, 100);
AddWrapPanelTop();
AddTextBoxTop();
AddTopButton();
}
void AddTextBoxTop()
{
TextBox txtB1 = new TextBox();
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
WrapPanel wp = (WrapPanel)topSP.Children[0];
wp.Children.Add(txtB1);
}
void AddWrapPanelTop()
{
WrapPanel myWrapPanel = new WrapPanel();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);
myWrapPanel.Background = System.Windows.Media.Brushes.Magenta;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.Width = 4000;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;
topSP.Children.Add(myWrapPanel);
}
void AddTopButton()
{
TextBox txtB1 = new TextBox();
txtB1.Background = System.Windows.Media.Brushes.Magenta;
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
topAddBut = new Button();
topAddBut.Click += new RoutedEventHandler(this.TopClick);
topAddBut.Content = "Add";
topAddBut.Width = 75;
// Add the buttons to the parent WrapPanel using the Children.Add method.
WrapPanel wp = (WrapPanel)topSP.Children[0];
wp.Children.Add(txtB1);
wp.Children.Add(loadBut);
this.topSP.Children.Add(wp);
}
void TopClick(Object sender, EventArgs e)
{
TextBox txtB1 = new TextBox();
txtB1.Background = System.Windows.Media.Brushes.Magenta;
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
Button s = (Button)sender;
WrapPanel wp = (WrapPanel)s.Parent;
wp.Children.Remove(s);
wp.Children.Add(txtB1);
AddTopButton();
// Add the buttons to the parent WrapPanel using the Children.Add method.
}
}
}
You define the following:
StackPanel topSP = null;
Then you have
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
This is in your local scope though so will be destroyed when you exit the function
Finally you have
topSP.Children.Add(myWrapPanel);
This will still be set to null which is why your error occurs. Basically you are creating a local version of the same variable.
In order to resolve simply change the second definition to:
topSP = new StackPanel { Width = 400, Height = 50 };
I try to render some elements on writeable bitmap. It works when rendering textblock but not something else for example rectangle. Why so?
void bm_ImageOpened(object sender, RoutedEventArgs e)
{
WriteableBitmap wbm = new WriteableBitmap((BitmapImage)sender);
TextBlock tb = new TextBlock();
tb.FontSize = 40;
tb.Text = "text";
Rectangle rt = new Rectangle();
rt.Width = 50;
rt.Width = 30;
rt.Fill = new SolidColorBrush(Colors.Red);
TranslateTransform tf = new TranslateTransform();
tf.X = 100;
tf.Y = 100;
wbm.Render(tb, tf); //this works
wbm.Render(rt, tf); //this not
wbmi.Invalidate();
}
You are trying to render a Rectangle with Height = 0 - you have defined its Width twice.
I suppose it should look like this:
rt.Width = 50;
rt.Height = 30;