In WPF:
<Button Width="24" Height="24" >
<Image Source="pack://application:,,,/res/x.png" VerticalAlignment="Center"/>
</Button>
How can I mimic this in C#? I can't find any method in the Button class that adds children.
Button is a Content control so you just have to use the Buttons Content property
Example:
Button myButton = new Button
{
Width = 24,
Height = 24,
Content = new Image
{
Source = new BitmapImage(new Uri("image source")),
VerticalAlignment = VerticalAlignment.Center
}
};
Related
I created a UWP solution and created a button. There are no problems with the buttons.
I do not know how to center the button in the window.
Code
StackPanel stackPanel1 = new StackPanel();
Button TestButton = new Button();
TestButton.Content = "Test";
stackPanel1.Children.Add(subscribeButton);
Window.Current.Content = stackPanel1;
This line is all you need.
TestButton.HorizontalAlignment = HorizontalAlignment.Center;
Changing the position of the button to half of the form's width and height will work.
Point point = new Point(this.Width / 2 - (button1.Width/2), this.Height / 2 - (button1.Height/ 2));
button1.Location = point;
The added - (button1.Width/2) is because the button's position is based on the upper right hand corner of the button, and not actually the center.
You can set HorizontalAlignment something like these inside the stackpanel...
<StackPanel Margin="10,0,0,0" Orientation="Horizontal" Grid.Column="0">
<TextBlock Grid.Column="1" Grid.Row="0" FontSize="16" HorizontalAlignment="Center">2. Draw a pattern</TextBlock>
<Button x:Name="DeleteSiteButton" Content="Delete Site"
HorizontalAlignment="Right" Margin="0,0,0,0" VerticalAlignment="Top" Click="DeleteSiteButton_Click"/>
<Button x:Name="AddSiteButton" Content="Add Site" HorizontalAlignment="Right"
Margin="10,0,0,0" VerticalAlignment="Top" Click="AddSiteButton_Click"/>
</StackPanel>
StackPanel control arranges child elements into a single line that can be oriented horizontally or vertically. When the Orientation property is set to Vertical(The default value is Vertical), the VerticalAlignment property of the button control will be invalid. Similarly, when the Orientation property is set to Horizontal, the HorizontalAlignment property of the button control will be invalid.
If you add a button control into a StackPanel, the HorizontalAlignment property and VerticalAlignment property of the button control will not work at the same time, that is, the button control can not be centered in a StackPanel.
We suggest that you could add the button control to a Grid panel, like this:
Grid gridPanel1 = new Grid();
Button TestButton = new Button();
TestButton.Content = "Test";
TestButton.HorizontalAlignment = HorizontalAlignment.Center;
TestButton.VerticalAlignment = VerticalAlignment.Center;
gridPanel1.Children.Add(TestButton);
Window.Current.Content = gridPanel1;
I have a tab control in which I am adding tab items programmatically. I want to have a close button with each tab item. On googling I found below XAML code for it:
<Button Content="X" Cursor="Hand" DockPanel.Dock="Right"
Focusable="False" FontFamily="Courier" FontSize="9"
FontWeight="Bold" Margin="5,0,0,0" Width="16" Height="16" />
Now, I am converting this code into equivalent C# code and struggling with some of the properties. Below given is the code which I have till now.
var CloseButton = new Button()
{
Content = "X",
Focusable = false,
FontFamily = FontFamily = new System.Windows.Media.FontFamily("Courier"),
FontSize = 9,
Margin = new Thickness(5, 0, 0, 0),
Width = 16,
Height = 16
};
I want help with properties like Cursor, DockPanel.Dock. Any help on this is much appreciated. Thanks !
Cursors are a fairly standard set of types. There are static classes out there that give you access to many of them. Use the Cursors class to get the Hand.
DockPanel.Dock is an attached property, it's not a property of the button control. You have to use the property setters for that dependency object or other convenience methods if available.
var button = new Button
{
Content = "X",
Cursor = Cursors.Hand,
Focusable = false,
FontFamily = new FontFamily("Courier"),
FontSize = 9,
Margin = new Thickness(5, 0, 0, 0),
Width = 16,
Height = 16
};
// this is how the framework typically sets values on objects
button.SetValue(DockPanel.DockProperty, Dock.Right);
// or using the convenience method provided by the owning `DockPanel`
DockPanel.SetDock(button, Dock.Right);
Then to set the bindings, create the appropriate binding object and pass it to the element's SetBinding method:
button.SetBinding(Button.CommandProperty, new Binding("DataContext.CloseCommand")
{
RelativeSource = new RelativeSource { AncestorType = typeof(TabControl) },
});
button.SetBinding(Button.CommandParameterProperty, new Binding("Header"));
In XAML, you can add a button like this:
<Button Height="Auto" Width="Auto" Background="Transparent" ToolTipServie.ToolTip="Tooltip here" ToolTipService.Placement="Bottom" />
However I cannot achieve similar approach with C#. Assume I have a Stackpanel named "toolbar":
toolbar.Children.Add(new Button {
Name = "undo",
FontFamily = new FontFamily("Segoe MDL2 Assets"),
Content = "",
Opacity = 100,
});
When I debug the code, it adds a button with squares, gray background. Also I don't know how to add a tooltip to the button.
I tried searching for help, nothing helped. :(
Any help is much appreciated. Thanks.
The tooltip isn't a normal DP, it's an attached property via the ToolTipService class, so you need to add it as such:
var button = new Button
{
Name = "undo",
FontFamily = new FontFamily("Segoe MDL2 Assets"),
Content = "",
Opacity = 100,
};
ToolTipService.SetToolTip(button, "Tooltip here");
ToolTipService.SetPlacement(button, PlacementMode.Bottom);
toolbar.Children.Add(button);
I've got a StackPanel which I need to fill in with ExpanderViews based on my data.
I am creating the ExpanderViews in code-behind and assigning to it, the DataTemplate present in the XAML.
The problem is, I am able to create the ExpanderView programmatically, but the DataTemplate approach doesn't work. All I can see is the "Expander Header" which doesn't show the items after click.
However, I can add Items manually to the ExpanderView and it shows the Items.
Please help!
C# Code:
ExpanderView expandOne = new ExpanderView()
{
Width = 400,
Margin = new Thickness(2),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Expander = new Border()
{
Width = 400,
Background = new SolidColorBrush(Colors.Brown),
Child = new TextBlock()
{
Text = "Expander Header",
FontSize = 34,
Foreground = new SolidColorBrush(Colors.Black),
Margin = new Thickness(40, 5, 5, 5),
},
},
};
// Assign DataTemplate
DataTemplate temp = (DataTemplate)FindName("ItemTemplateName");
expandOne.ItemTemplate = temp;
// add ExpanderView to StackPanel
this.MyStackPanel.Children.Add(expandOne);
XAML Code:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="ItemTemplateKey" x:Name="ItemTemplateName">
<ListBox Grid.Row="0" x:Name="ItemListBox">
<ListBox.Items>
<TextBlock Text="Filter Content 1" Foreground="Black"/>
<TextBlock Text="Filter Content 2" Foreground="Black"/>
<TextBlock Text="Filter Content 3" Foreground="Black"/>
</ListBox.Items>
</ListBox>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
If anyone still looking for an answer, I solved this by using the solution mentioned in
DataBinding to WP8 Toolkit ExpanderView
How to add a StackPanel in a Button using c# code behind (i.e. convert the following XAML to C# )? There is no Button.Children.Add...
<Button>
<StackPanel Orientation="Horizontal" Margin="10">
<Image Source="foo.png"/>
</StackPanel>
</Button>
Image img = new Image();
img.Source = new BitmapImage(new Uri("foo.png"));
StackPanel stackPnl = new StackPanel();
stackPnl.Orientation = Orientation.Horizontal;
stackPnl.Margin = new Thickness(10);
stackPnl.Children.Add(img);
Button btn = new Button();
btn.Content = stackPnl;
Set Button.Content instead of using Button.Children.Add
As a longer explanation:
Button is a control which "only has 1 child" - its Content.
Only very few controls (generally "Panels") can contain a list of zero or more Children - e.g. StackPanel, Grid, WrapPanel, Canvas, etc.
As your code already shows, you can set the Content of a Button to be a Panel - this would ehn allow you to then add multiple child controls. However, really in your example, then there is no need to have the StackPanel as well as the Image. It seems like your StackPanel is only adding Padding - and you could add the Padding to the Image rather than to the StackPanel if you wanted to.
Use like this
<Window.Resources>
<ImageSource x:Key="LeftMenuBackgroundImage">index.jpg</ImageSource>
<ImageBrush x:Key="LeftMenuBackgroundImageBrush"
ImageSource="{DynamicResource LeftMenuBackgroundImage}"/>
</Window.Resources>
and in Codebehind
Button btn = new Button();
btn.HorizontalContentAlignment = HorizontalAlignment.Stretch;
btn.VerticalContentAlignment = VerticalAlignment.Stretch;
StackPanel stk = new StackPanel();
stk.Orientation = Orientation.Horizontal;
stk.Margin = new Thickness(10, 10, 10, 10);
stk.SetResourceReference(StackPanel.BackgroundProperty, "LeftMenuBackgroundImageBrush");
btn.Content = stk;
In Xaml :
<Button x:Name="Btn" Click="Btn_Click" Orientation="Horizontal" Margin="10">
<StackPanel>
<Image Source="foo.png" Height="16" Width="16"/>
</StackPanel>
</Button>
In C# :
Button btn = new Button();
StackPanel panel = new StackPanel();
Image img = new Image
{
Source = "../foo.png"
}
panel.Children.Add(img);
btn.Content = panel;
I advise you to put the image in xaml resources :
<Window.Resources>
<BitmapImage x:Key="Img" UriSource="/Img/foo.png"/>
</Window.Resources>
And call it like this :
Image img = new Image
{
Source = (BitmapImage)FindResource("Img")
};