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?
Related
There are 2 part for setting style of button.
The 1st is BackgroundProperty by Style.Setters. It's working well.
The 2nd is BackgroundProperty by Style.Triggers which is run by that Mouse is over a button. But, It's not working.
// the 1st BackgroundProperty by Style.Setters
Style style = new Style(typeof(Button));
style.Setters.Add(new Setter(Button.BackgroundProperty, imageSourceOn));
// the 2nd BackgroundPropert by Style.Triggers
Setter setter = new Setter();
setter.Property = Button.BackgroundProperty;
setter.Value = imageSourceOff;
Trigger trigger = new Trigger();
trigger.Property = IsMouseOverProperty;
trigger.Value = true;
trigger.Setters.Add(setter);
style.Triggers.Add(trigger);
Button button = new Button();
button.Margin = new Thickness(0, 5, 80, 5);
button.BorderThickness = new Thickness(0);
button.Name = name;
button.Click += handler;
button.VerticalContentAlignment = VerticalAlignment.Bottom;
// setting Style
button.Style = style;
return button;
Thanks Ed Plunkett.
I added ControlTemplate to override the control's background brush property.
Style style = new Style(typeof(Button));
style.Setters.Add(new Setter(Button.BackgroundProperty, imageSourceOn));
This is the ControlTemplate part what I add:
ControlTemplate template = new ControlTemplate(typeof(Button));
FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Border));
elemFactory.Name = "Border";
elemFactory.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Button.BackgroundProperty));
template.VisualTree = elemFactory;
elemFactory.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter)));
And, It is trigger part.
Setter setter = new Setter();
setter.Property = Button.BackgroundProperty;
setter.Value = imageSourceOff;
Trigger trigger = new Trigger();
trigger.Property = IsMouseOverProperty;
trigger.Value = true;
trigger.Setters.Add(setter);
Finally, I set my ControlTemplate.
template.Triggers.Add(trigger);
setter = new Setter();
setter.Property = Button.TemplateProperty;
setter.Value = template;
style.Setters.Add(setter);
It works well.
I want a grid layout for displaying search results. The grids should have headlines Beneficial Owner, Commercial Operator and Registered Owner. The results should then be displayed under the right headline.
I am trying to achieve this layout:
But this is what i get:
My C#/SilverLight code:
// Create a 3 column grid
StackPanel deptStackPanel = new StackPanel();
deptStackPanel.Margin = new Thickness(10);
stackPanelSearchResults.Children.Add(deptStackPanel);
Grid.SetColumn(deptStackPanel, 3);
Grid.SetRow(deptStackPanel, 3);
// Add headlines for theese columns
TextBlock deptListHeadingBeneficialOwner = new TextBlock();
deptListHeadingBeneficialOwner.Text = "Beneficial Owner";
TextBlock deptListHeadingCommercialOperator = new TextBlock();
deptListHeadingCommercialOperator.Text = "Commercial Operator";
TextBlock deptListHeadingRegisteredOwnerName = new TextBlock();
deptListHeadingRegisteredOwnerName.Text = "Registered Owner";
deptStackPanel.Children.Add(deptListHeadingBeneficialOwner);
deptStackPanel.Children.Add(deptListHeadingCommercialOperator);
deptStackPanel.Children.Add(deptListHeadingRegisteredOwnerName);
Grid.SetColumn(deptListHeadingBeneficialOwner, 0);
Grid.SetColumn(deptListHeadingCommercialOperator, 1);
Grid.SetColumn(deptListHeadingRegisteredOwnerName, 2);
Add three grids inside the layout grid from your xaml file or from the back end. Put a stack panel inside that and then add the text blocks for your data. You are just adding text blocks one below the other.
I found the solution my self:
// Create the Grid
Grid myGrid = new Grid();
myGrid.Width = 400;
myGrid.Margin = new Thickness(9, 0, 0, 0);
myGrid.HorizontalAlignment = HorizontalAlignment.Left;
myGrid.VerticalAlignment = VerticalAlignment.Top;
myGrid.ShowGridLines = true;
// Define the Columns
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
myGrid.ColumnDefinitions.Add(colDef2);
myGrid.ColumnDefinitions.Add(colDef3);
// Define the Rows
RowDefinition rowDef1 = new RowDefinition();
RowDefinition rowDef2 = new RowDefinition();
RowDefinition rowDef3 = new RowDefinition();
RowDefinition rowDef4 = new RowDefinition();
myGrid.RowDefinitions.Add(rowDef1);
myGrid.RowDefinitions.Add(rowDef2);
myGrid.RowDefinitions.Add(rowDef3);
myGrid.RowDefinitions.Add(rowDef4);
// Add the second text cell to the Grid
TextBlock txtBeneficialOwner = new TextBlock();
txtBeneficialOwner.Text = "Beneficial Owner";
txtBeneficialOwner.FontWeight = FontWeights.Bold;
Grid.SetRow(txtBeneficialOwner, 0);
Grid.SetColumn(txtBeneficialOwner, 0);
// Add the third text cell to the Grid
TextBlock txtCommercialOperator = new TextBlock();
txtCommercialOperator.Text = "Commercial Operator";
txtCommercialOperator.FontWeight = FontWeights.Bold;
txtCommercialOperator.Margin = new Thickness(9, 0, 0, 4);
Grid.SetRow(txtCommercialOperator, 0);
Grid.SetColumn(txtCommercialOperator, 1);
// Add the fourth text cell to the Grid
TextBlock txtRegisteredOwnerName = new TextBlock();
txtRegisteredOwnerName.Text = "Registered Owner";
txtRegisteredOwnerName.FontWeight = FontWeights.Bold;
txtRegisteredOwnerName.Margin = new Thickness(9, 0, 0, 4);
Grid.SetRow(txtRegisteredOwnerName, 0);
Grid.SetColumn(txtRegisteredOwnerName, 2);
// Add the TextBlock elements to the Grid Children collection
myGrid.Children.Add(txtBeneficialOwner);
myGrid.Children.Add(txtCommercialOperator);
myGrid.Children.Add(txtRegisteredOwnerName);
stackPanelSearchResults.Children.Add(myGrid);
I am creating Label, Textbox and a button dynamically. I need Button to appear in the same line as textbox to its right.
This is the code i am using:
Label lbl = new Label()
{
Content = "Some Label",
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Height = 28,
};
TextBox tb = new TextBox()
{
Text = "Some Text",
IsReadOnly = true,
};
Button btn = new Button()
{
Content = "Click Me",
HorizontalAlignment = HorizontalAlignment.Left
Margin = new Thickness(tb.ActualWidth),
};
I am assigning Button Margin to the Right of TextBox but it still appears in the next line under the textbox.
What am i doing wrong here?
You can use StackPanel to solve your problem:
StackPanel spMain = new StackPanel() { Orientation = Orientation.Vertical };
Label lbl = new Label()
{
Content = "Some Label",
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Height = 28,
};
StackPanel spInner = new StackPanel() { Orientation = Orientation.Horizontal };
TextBox tb = new TextBox()
{
Text = "Some Text",
IsReadOnly = true,
};
Button btn = new Button()
{
Content = "Click Me",
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(tb.ActualWidth),
};
spInner.Children.Add(tb);
spInner.Children.Add(btn);
spMain.Children.Add(lbl);
spMain.Children.Add(spInner);
You can check following link for more information:
http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.orientation.aspx
It depends on what the page content that you are placing the controls on is, is it a grid or something else?
Why not also create a stackpanel which will properly contain your items in the fashion as needed.
Why don't you place all the created controls in a StackPanel with its Orientation set to Horizontal. This way they will always placed next to eachother.
var stPanel = new StackPanel { Orientation = Orientation.Horizontal };
var button = new Button() { ... }
stPanel.Children.Add(button);
//And so on
Edit: kmatyaszek was ahead of me... :)
i think it would be better if you use the Content methode from the instance of Button
private byte _count;
internal void FillbtnSubCat(Grid grid)
{
var myDefinition = new ColumnDefinition();
var myButton = new Button();
var myBlock = new TextBlock()
{
TextAlignment = TextAlignment.Center,
TextWrapping = TextWrapping.Wrap,
Text = "Some Text",
Margin = new Thickness(5, 10, 5, 10)
};
Grid.SetColumn(myButton, _count);
myButton.Margin = new Thickness(5, 10, 5, 25);
myButton.MinWidth = 30;
myButton.Content = myBlock;
myDefinition.Width = new GridLength(68);
grid.ColumnDefinitions.Add(myDefinition);
grid.Children.Add(myButton);
_count++;
}
XAML
<Grid Name="Grid1" Height="100" Width="auto">
</Grid>
I am creating the DataGridTemplateColumn dynamically.
var binding = new Binding
{
Path = new PropertyPath("MyProperty"),
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus
};
var converterParameter = new List<object> { header, rows, myGrid };
binding.Converter = new MyConverter();
binding.ConverterParameter = converterParameter;
var textBoxValue = new FrameworkElementFactory(typeof(TextBox));
totalUnitsValue.SetBinding(TextBox.TextProperty, binding);
totalUnitsValue.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Right);
totalUnitsValue.SetValue(TextBox.WidthProperty, 40.0);
totalUnitsValue.SetValue(TextBox.MarginProperty, new Thickness(4, 0, 10, 0));
var factoryElement = new FrameworkElementFactory(typeof(StackPanel));
factoryElement.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
factoryElement.AppendChild(textBoxValue );
var column = new DataGridTemplateColumn
{
Header = header,
CellTemplate = new DataTemplate { VisualTree = factoryElement }
};
myGrid.Columns.Add(column);
This works fine for few columns. But if i create 10 or more columns (80 -90) textBoxes then the last created TextBoxes do not allow me to change the value or do not allow me to put focus on the TextBox. It becomes like TextBlock.
EDIT:
IF I REMOVE THE STACKPANEL, THEN THERE IS NO ISSUE WITH THE TEXTBOX BUT I NEED TO SHOW MORE THAN ONE ELEMENT, SO I NEED TO HAVE SOME SORT OF CONTAINER.ANY HELP ON THAT.
Please guide what could be tghe
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);