I'm trying to set the following properties in the C# code behind of StackPanel that I need to add programmatically:
BorderThickness
BorderBrush
Any idea on how to set these programmatically?
I know this is a year later, but I have found an answer in case someone still needs it.
Here is a complete example
// Create a StackPanel and Add children
StackPanel myStackPanel = new StackPanel();
Border myBorder1 = new Border();
myBorder1.Background = Brushes.SkyBlue;
myBorder1.BorderBrush = Brushes.Black;
myBorder1.BorderThickness = new Thickness(1);
TextBlock txt1 = new TextBlock();
txt1.Foreground = Brushes.Black;
txt1.FontSize = 12;
txt1.Text = "Stacked Item #1";
myBorder1.Child = txt1;
Border myBorder2 = new Border();
myBorder2.Background = Brushes.CadetBlue;
myBorder2.Width = 400;
myBorder2.BorderBrush = Brushes.Black;
myBorder2.BorderThickness = new Thickness(1);
TextBlock txt2 = new TextBlock();
txt2.Foreground = Brushes.Black;
txt2.FontSize = 14;
txt2.Text = "Stacked Item #2";
myBorder2.Child = txt2;
// Add the Borders to the StackPanel Children Collection
myStackPanel.Children.Add(myBorder1);
myStackPanel.Children.Add(myBorder2);
mainWindow.Content = myStackPanel;
The StackPanel does not have a BorderThickness or BorderBrush properties. Only Background. If you want to set those, you would need to wrap the StackPanel in a Border control:
<Border x:Name="StackBorder">
<StackPanel>
</Border>
You can then call:
StackBorder.BorderThickness = new Thickness(1);
StackBorder.BorderBrush = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Red);
You cannot set border properties on the StackPanel object itself. You place the StackPanel object inside a Border object and set the BorderThickness and BorderBrush properties on the Border object with something like:
myBorder.BorderBrush = Brushes.Black;
myBorder.BorderThickness = new Thickness(1);
Related
I am trying to read an "ini" file and to create multiple radiobuttons depending on the number of the lines of this file. The code used is the following:
private void CreateRadioButtons(string filePath)
{
StackPanel mainStackPanel = new StackPanel();
mainStackPanel.Orientation = Orientation.Vertical;
mainStackPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
mainStackPanel.VerticalAlignment = VerticalAlignment.Stretch;
var lines = File.ReadAllLines(filePath).Where(l => !l.StartsWith(";"));
WrapPanel wrapPanel = new WrapPanel();
wrapPanel.Margin = new Thickness(30);
ScrollViewer scrollViewer = new ScrollViewer();
Style style = new Style(typeof(RadioButton));
style.Setters.Add(new Setter(RadioButton.BackgroundProperty, Brushes.Yellow));
style.Setters.Add(new Setter(RadioButton.BorderThicknessProperty, new Thickness(2)));
ControlTemplate controlTemplate = new ControlTemplate(typeof(RadioButton));
FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
border.SetValue(Border.BorderThicknessProperty, new TemplateBindingExtension(BorderThicknessProperty));
border.SetValue(Border.BorderBrushProperty, new TemplateBindingExtension(BorderBrushProperty));
border.SetValue(Border.BackgroundProperty, Brushes.Transparent);
border.SetValue(Border.CornerRadiusProperty, new CornerRadius(20));
border.SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Blue));
border.SetValue(Border.BackgroundProperty, Brushes.Transparent);
FrameworkElementFactory image = new FrameworkElementFactory(typeof(Image));
image.SetValue(Image.SourceProperty, new BitmapImage(new Uri("pack://application:,,,/Resources/image.jpg")));
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.Name = "TextBlock";
textBlock.SetValue(TextBlock.TextProperty, "Text");
textBlock.SetValue(TextBlock.MarginProperty, new Thickness(5));
FrameworkElementFactory secondaryStack = new FrameworkElementFactory(typeof(StackPanel));
secondaryStack.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);
secondaryStack.SetValue(StackPanel.MarginProperty, new Thickness(5, 10, 5, 0));
secondaryStack.AppendChild(image);
secondaryStack.AppendChild(textBlock);
border.AppendChild(secondaryStack);
controlTemplate.VisualTree = border;
style.Setters.Add(new Setter(Control.TemplateProperty, controlTemplate));
Trigger trigger = new Trigger() { Property = RadioButton.IsCheckedProperty, Value = false };
trigger.Setters.Add(new Setter(UIElement.OpacityProperty, 0.5));
style.Triggers.Add(trigger);
FrameworkElementFactory label = new FrameworkElementFactory(typeof(Label));
foreach (var line in lines)
{
RadioButton radioButton = new RadioButton();
radioButton.Style = style;
radioButton.Width = 135;
radioButton.Height = 125;
radioButton.Margin = new Thickness(15, 0, 0, 15);
radioButton.Content = border;
wrapPanel.Children.Add(radioButton);
}
This code is functioning at the moment. The problem comes when i try to implement textblocks with dynamic text: I just need the text inside the radiobutton to be equal to the string line. I have tried multiple ways but in the end I only encounter exceptions about windowspresentation (which I think mean that the elements are presented in the wrong way). How can I make textBlock dynamic?
I am creating a Button control using C# as mentioned below in the code. I have created the rounded border for the button style. I am not able to see any property to assign the Border in the button.
var button = new System.Windows.Controls.Button
{
Name = "BtnOk",
Content = "OK",
Height = 20,
Width = 60,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.DarkGray,
Foreground = Brushes.WhiteSmoke,
Margin = new Thickness(0,0,0,5)
};
Border border = new Border();
border.CornerRadius = new CornerRadius(3);
How can I apply Border in button programatically?
a button cannot aplly a border. a border can decorate a button:
border.Child = button;
usually Buttons already have a Border inside their Template (ControlTemplate). that Border isn't easily accessible - there is no special property of Button class, but that border can be found in visual tree after template was loaded.
additionally that Border can be customized by default style if you put it in Button.Resources. change CorderRadius using Style.Setter:
var button = new System.Windows.Controls.Button
{
Name = "BtnOk",
Content = "OK",
Height = 20,
Width = 60,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.DarkGray,
Foreground = Brushes.WhiteSmoke,
Margin = new Thickness(0, 0, 0, 5)
};
var style = new Style
{
TargetType = typeof(Border),
Setters = { new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(3) } }
};
button.Resources.Add(style.TargetType, style);
or using object/collection initializers:
var button = new System.Windows.Controls.Button
{
Name = "BtnOk",
Content = "OK",
Height = 20,
Width = 60,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.DarkGray,
Foreground = Brushes.WhiteSmoke,
Margin = new Thickness(0, 0, 0, 5),
Resources =
{
{
typeof(Border), new Style
{
TargetType = typeof(Border),
Setters =
{
new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(13) }
}
}
}
}
};
if many buttons should have different CornerRadius, changing Button's Template can be a solution. Change template and set CornerRadius as attached dependency property, like shown in this post: Set a property of a nested element in an WPF style
There is indeed a Border element in the default ControlTemplate for the Button but the easiest way to set the CornerRadius property of it, without having to define a custom template, is to wait until the Button has been loaded and then get a reference to it. Try this:
var button = new System.Windows.Controls.Button
{
Name = "BtnOk",
Content = "OK",
Height = 20,
Width = 60,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.DarkGray,
Foreground = Brushes.WhiteSmoke,
Margin = new Thickness(0, 0, 0, 5)
};
button.Loaded += (ss, ee) =>
{
Border border = button.Template.FindName("border", button) as Border;
if (border != null)
border.CornerRadius = new CornerRadius(3);
};
Is it possible to set for each side of a border its own EventHandler for mouse-enter or mouse-leave event. For example for the Left-Border of a Grid and Top-Border of a Grid?
What I am actually trying to do is allow the user to resize Grid-Elements inside a Canvas that contain a TextBlock with the mouse.
I am inserting my Grid/Border into the Canvas with the following code:
Border border = new Border();
border.BorderThickness = new Thickness(2);
border.BorderBrush = Brushes.Black;
TextBlock tb = new TextBlock();
tb.HorizontalAlignment = HorizontalAlignment.Stretch;
tb.TextWrapping = TextWrapping.Wrap;
tb.Padding = new Thickness(5, 5, 5, 5);
tb.Text = fd.LabelText;
Grid grid = new Grid();
grid.Background = labelBackgroundBrush;
grid.Background.Opacity = myOpactiy;
border.DataContext = fd;
grid.Children.Add(tb);
border.Child = grid;
I found a good example at csharphelper.com . Although my implementation is still buggy this was a good inspiration for me. Maybe it can help others who want to do the same.
Hello I have a textblock within a grid row. The text outside the width of the row is not shown. How can i split the text and display it underneath?
//Actual -->
//Date: 06/06/2018 realy long text is displayed, b
//Wanted -->
//Date: 06/06/2018 realy long text is displayed,
//but not showing everything due to width
//isue
Grid DynamicGrid = new Grid();
DynamicGrid.Width = 450;
DynamicGrid.Height = 1000;
DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
DynamicGrid.ShowGridLines = false;
RowDefinition gridRow;
gridRow = new RowDefinition();
gridRow.Height = new GridLength(50);
DynamicGrid.RowDefinitions.Add(gridRow);
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = "06/06/2018 realy long text is displayed, but not showing everything due to width isue";
txtBlock1.FontSize = 20;
txtBlock1.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock1, 0);
Grid.SetColumn(txtBlock1, 0);
DynamicGrid.Children.Add(txtBlock1);
Setting the TextWrapping property of the TextBlock to wrap will cause the textblock to show full text.
I created a border and a label by code in c# wpf
I want to create a data binding for Background and BorderBrush of border with
the Background of the label
Here is the code
void creatlbl()
{
Border b = new Border();
b.Name = "b11";
b.Margin = new Thickness(300,17,0,419);
b.Height = 32;
b.CornerRadius = new CornerRadius(5);
b.Width = 181;
b.BorderThickness = new Thickness(2);
b.HorizontalAlignment = HorizontalAlignment.Left;
lgingrd.Children.Add(b); //
Label l = new Label();
l.Name = "l111";
l.Content = "l111";
l.Height = 28;
l.Width = 177;
l.Foreground = (Brush)bc.ConvertFrom("#FF346D80");
l.FontSize = 20;
l.Background = (Brush)bc.ConvertFrom("#FF9AB426");
l.HorizontalContentAlignment = HorizontalAlignment.Center;
l.Padding = new Thickness(0,0,0,0);
l.Visibility = Visibility.Visible;
b.Child = l;
// here i want to set binding for border
// the background and borderbrush of border equal to the background of label
}
same thing i did for a button in XAML like that
<Border x:Name="brdbt" Margin="120,58,0,378"
BorderBrush="{Binding Background, ElementName=bt}" <!--this is the binding which i want-->
Height="32"
CornerRadius="5"
Width="181"
BorderThickness="2"
HorizontalAlignment="Left" Background="{Binding Background, ElementName=bt}">
<Button x:Name="bt" Content="btntxt" HorizontalAlignment="Left" VerticalAlignment="Top" Height="28" Width="177" Click="bt_click" BorderBrush="{x:Null}" Foreground="#FFE8EEF0" FontSize="20" Padding="1,-1,1,1" MouseEnter="bt_mcentr" MouseLeave="bt_mclv" Background="#FFE62828"/>
</Border>
Is there any way to do above xaml type binding in c# code for border and label I mentioned above in c# code?
create Binding object with path "Background" which uses label l as source (new Binding("Background") { Source = l }) and assign that binding to target properties: Border.Background and Border.BorderBrush
BindingOperations.SetBinding(b, BackgroundProperty, new Binding("Background") { Source = l });
BindingOperations.SetBinding(b, BorderBrushProperty, new Binding("Background") { Source = l });
You know it would be simpler to create this entire block of code in xaml.
Just wrap it in DataTemplate with key.
In your code, create a ContentPresenter, give it your data as content,
And give it your DataTemplate as it's ContentTemplate value:
ContentPresenter cp = new ContentPresenter();
cp.Content = (?)
cp.ContentTemplate = Application.Current.Resources.Find("DataTemplateKey") as DataTemplate;