I am programatically constructing a block of XAML and would like to provide a name x:Name to a TextElement object (an Underline in this case) but even though the Name prop provides a setter, it can not be set per this MSDN article:
MSDN: TextElement.Name
Gets or sets a unique identification for the object. Name can only be set from initial parsing of XAML.
Here is my code:
public void AddLink(string token, string text, string uri)
{
var elem = new Underline();
elem.Name.Name = token; // <-- I would expect this would work...
elem.Inlines.Add();
if (Container == null)
Container = new Paragraph();
Container.Inlines.Add(elem);
}
I am not sure if I get your question right but to me it seems you are asking to register a name to specific namescope in wpf.
This is what you need:
MSDN: FrameworkElement.RegisterName
Here is an example:
myMainPanel = new StackPanel();
myMainPanel.Background = Brushes.Orange;
button1 = new Button();
button1.Name = "Button1";
// Register button1's name with myMainPanel.
myMainPanel.RegisterName(button1.Name, button1);
button1.Content = "Button 1";
button1.Click += new RoutedEventHandler(button1Clicked);
myMainPanel.Children.Add(button1);
button2 = new Button();
button2.Name = "Button2";
// Register button2's name with myMainPanel.
myMainPanel.RegisterName(button2.Name, button2);
button2.Content = "Button 2";
button2.Click += new RoutedEventHandler(button2Clicked);
myMainPanel.Children.Add(button2);
Related
I'm new to c#, and I do know how to dynamically add controls, but I don't know how to set that control as this.[control_name]. Note that here, this is the Form.
This can be done statically with private System.Windows.Forms.[Control_type] [control-name];, but how would i do this from a method so that I can later declare this.[control-name] = [variable]?
Note here, variable would be something like new TextBox();
var txt = new TextBox(); //txt is the variable you are looking for
Form1.Controls.Add(txt); //added it to the form
Now you can access it by txt:
txt.Location = new Point(0,0);
txt.Visible = true;
If you create the control inside a method (as you mentioned in the comments), you can return and use it like below:
public TextBox AddTextBox()
{
var txt = new TextBox();
Form1.Controls.Add(txt);
return txt;
}
var newTxt = AddTextBox();
newTxt.Location = new Point(0,0);
newTxt.Visible = true;
Form2:
private ToolStripMenuItem mHelp;
private ToolStripMenuItem apProposToolStripMenuItem;
public void intializecomponent()
{
this.mHelp = new ToolStripMenuItem();
this.contentsToolStripMenuItem = new ToolStripMenuItem();
this.apProposToolStripMenuItem = new ToolStripMenuItem();
this.mHelp.DropDownItems.AddRange(new ToolStripItem[2]
{
(ToolStripItem) this.contentsToolStripMenuItem,
(ToolStripItem) this.apProposToolStripMenuItem
});
this.mHelp.Name = "mHelp";
this.mHelp.Size = new Size(44, 20);
this.mHelp.Text = "Help";
this.contentsToolStripMenuItem.Name = "contentsToolStripMenuItem";
this.contentsToolStripMenuItem.Size = new Size(122, 22);
this.contentsToolStripMenuItem.Text = "Contents";
this.contentsToolStripMenuItem.Click += new EventHandler(this.contentsToolStripMenuItem_Click);
this.apProposToolStripMenuItem.Image = (Image) componentResourceManager.GetObject("apProposToolStripMenuItem.Image");
this.apProposToolStripMenuItem.Name = "apProposToolStripMenuItem";
this.apProposToolStripMenuItem.Size = new Size(122, 22);
this.apProposToolStripMenuItem.Text = "About";
this.apProposToolStripMenuItem.Click += new EventHandler(this.apProposToolStripMenuItem_Click);
this.Load += new EventHandler(this.DocumentSpace_Load);
}
How to find apProposToolStripMenuItem on the form? I tried to remove a particular ToolStripMenuItem, but it doesn't work and I can't find apProposToolStripMenuItem.
Form1:
ToolStripMenuItem mi = new ToolStripMenuItem("apProposToolStripMenuItem") { Name = "About" };
mi.DropDownItems.RemoveByKey("About");
You can remove it by name like this:
mHelp.DropDownItems.RemoveByKey("apProposToolStripMenuItem");
You can also remove it directly like this:
var about = mHelp.DropDownItems["apProposToolStripMenuItem"]
mHelp.DropDownItems.Remove(about);
Assuming you have access to the MenuStrip or the ToolStrip on the form, then you can use Descendants extension method to find all items, regardless of its location in hierarchy of menus and its parent item. for example:
var item = menuStrip1.Descendants()
.Where(x => x.Name == "printToolStripMenuItem").FirstOrDefault();
item?.GetCurrentParent().Items.Remove(item);
im really new to c# and Xamarin Forms but can someone please explain to me why I'm getting an error in the last line of code below ?
Visual Studio is indicating a curly red underline on the word Children on the last line of this code indicating that there is some syntax error but I don't see why that is an error since I'm just accessing a member of the class ....
var content = new ContentPage();
content.Title = "Appuler";
Label CompanyName = new Label();
CompanyName.HorizontalTextAlignment = TextAlignment.Center;
CompanyName.Text = "Test";
Button NextPage = new Button();
NextPage.Text = "Next Page";
NextPage.Font = Font.SystemFontOfSize(NamedSize.Large);
NextPage.BorderWidth = 1;
NextPage.HorizontalOptions = LayoutOptions.Center;
NextPage.VerticalOptions = LayoutOptions.CenterAndExpand;
content.Content = new StackLayout();
content.Content.VerticalOptions = LayoutOptions.CenterAndExpand;
content.Content.Children.Add(CompanyName);
Refactor the last few lines to the following
var layout = new StackLayout();
layout.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.Children.Add(CompanyName);
content.Content = layout;
ContentPage.Content does not have that Children property as it is a View. StackLayout, however does have a Children property that can be accessed.
populate the layout control's accessible properties and then assign it to the Content property of the ContentPage.
I'm using C# and asp web forms. I'm trying to build a table dynamically. Some cells of my table need to have buttons.
Everything was working fine using a more verbose c#. When I try to use literal to simplify my code I find a weird thing. I can't access the Click event inside the literal to setup the event handler.
First, code that was working!
var row = new TableRow();
var cell = new TableCell();
var btn = new Button();
btn.CausesValidation = false;
btn.UseSubmitBehavior = true;
btn.Text = "Editar";
btn.ID = item.Id.ToString();
btn.CommandName = "excluir";
btn.CommandArgument = item.Id.ToString();
btn.CssClass = "btn btn-primary btn-xs";
//Click event is accessible this way!!!
btn.Click += new EventHandler(btnEditarArquivo_OnClick);
cell.Controls.Add(btn);
row.Cells.Add(cell);
this.table.Rows.Add(row);
Now using literals, the click event don't exist inside the literal.
What can I do?
this.table.Rows.Add(new TableRow(){
Cells ={
new TableCell() {
Controls = {
new Button() {
CausesValidation = false,
UseSubmitBehavior = true,
Text = "Editar",
ID = item.Id.ToString(),
CommandName = "excluir",
CommandArgument = item.Id.ToString(),
CssClass = "btn btn-primary btn-xs",
//No click event accessible, this does not work
Click += new EventHandler(btnEditarArquivo_OnClick)
}
}
}
}
});
What can I do?
Is there any way to make a self reference to access the click event?
I don't believe there is syntax sugar for events similar to properties.
I'd recommend refactor initialization of the button into separate function to make code more compact.
Note that
var btn = new Button();
btn.CausesValidation = false;
is exactly the same as
var btn = new Button() { CausesValidation = false };
it is just different syntax of setting property right after calling constructor. So unless the reason to switch syntax "I like it that way more" than you should not change it (i.e. there is no performance impact of any kind).
This is how i did. Using literals I don't need to reassign variables for each cell. But because of the click event I have to create a variable just for the buttons.
this.table.Rows.Clear();//clear table
//Re add headers
this.table.Rows.Add (
new TableHeaderRow(){ Cells = {
new TableHeaderCell() {Text = "Id"},
new TableHeaderCell() {Text = "Descrição"},
new TableHeaderCell() {Text = "Arquivo"},
new TableHeaderCell() {Text = "Tamanho"},
new TableHeaderCell() {Text = "Ed"},
new TableHeaderCell() {Text = "Ex"},
}
});
foreach (var item in _listaArquivos) {
var btn = new Dictionary<string,Button>();
btn["Editar"] = new Button() {
CausesValidation = false,
UseSubmitBehavior = true,
Text = "Edit",
ID = item.Id.ToString(),
CommandName = "edit",
CommandArgument = item.Id.ToString(),
CssClass = "btn btn-primary btn-xs",
};
btn["Excluir"] = new Button() {
CausesValidation = false,
UseSubmitBehavior = true,
Text = "Excluir",
ID = item.Id.ToString(),
CommandName = "excluir",
CommandArgument = item.Id.ToString(),
CssClass = "btn btn-primary btn-xs",
};
//add events
btn["Edit"].Click += new EventHandler(btnEditarArquivo_OnClick);
btn["Exclude"].Click += new EventHandler(btnEditarArquivo_OnClick);
this.tabelaArquivo.Rows.Add(
new TableRow() { Cells = {
new TableCell() {Text = item.Id.ToString()},
new TableCell() {Text = item.Descricao},
new TableCell() {Text = item.Arquivo},
new TableCell() {Text = item.Tamanho},
new TableCell() {Controls = {btn["Edit"]}},
new TableCell() {Controls = {btn["Exclude"]}},
},
});
}
I'm trying to use the Microsoft ribbon control programatically using C#. Everything is fine but I'm unable to bind the command through the RibbonCommand. Can anyone give me an example of how to do this? My actual code is:
Ribbon rbn = new Ribbon();
RibbonTab file = new RibbonTab();
file.Name = "file";
file.Label = "FILE";
RibbonTab edit = new RibbonTab();
edit.Name = "edit";
edit.Label = "Edit";
RibbonGroupPanel rbgp = new RibbonGroupPanel();
RibbonGroup rbg = new RibbonGroup();
RibbonButton rbtn = new RibbonButton();
rbtn.Content = "New";
RibbonCommand rcomnd = new RibbonCommand();
rcomnd.LabelTitle = "NEW";
rcomnd.ToolTipDescription = "THIS IS NEW";
rcomnd.LargeImageSource = imgSource;
rcomnd.Execute(rbtn, rbtn);
rbtn.IsEnabled = true;
//rcomnd.SmallImageSource = imgSource;
rcomnd.CanExecute +=new CanExecuteRoutedEventHandler(rcomnd_CanExecute);
rcomnd.Executed +=new ExecutedRoutedEventHandler(rcomnd_Executed);
CommandBinding cmdb = new CommandBinding(ApplicationCommands.New);
cmdb.Command = ApplicationCommands.New;
cmdb.Executed +=new ExecutedRoutedEventHandler(cmdb_Executed);
CommandBind.Add(cmdb);
//rcomnd.Executed += new ExecutedRoutedEventHandler(OnAddNewEntry);*/
rbtn.Click +=new System.Windows.RoutedEventHandler(rbtn_Click);
rbtn.Command = rcomnd;
But the bindings are not working and the button is not enabled.
Check this tutorial from the "Adding Commands" section. It may be good to read it from the start.