I have a little problem. I am enabling the user to choose the size of a textblock in which he is able to display text by doing some other stuff.
My problem here is that I have to add a border to the textblock to show the user how big it became.
When I applied the following code, my program just crashes in that scenario:
//create a TextBlock at desired position
TextBlock tmpTextBlock = new TextBlock
{
Width = 166,
Height = Math.Max(tmpY1, tmpY2) - Math.Min(tmpY1, tmpY2),
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(0, Math.Min(tmpY1, tmpY2) - 50, 0, (int)WeekGrid.ActualHeight - Math.Max(tmpY1, tmpY2)),
Text = "Type stuff here"
};
tmpTextBlock.Holding += tmpTextBox_Holding;
tmpTextBlock.RightTapped += tmpTextBox_RightTapped;
WeekGrid.Children.Add(tmpTextBlock);
Grid.SetRow(tmpTextBlock, 1);
//add the border - these lines produce the problem
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
The Exception that follows is an argument exception:
Value does not fall within the expected range.
EDIT
Whoops I've solved that problem. I had to remove adding the Textblock into the grid.
The problem I have now is that the border appears around the grid - not around the textblock!
The following code made that possible:
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
Padding = new Thickness(24)
};
WeekGrid.Children.Add(border);
Grid.SetRow(border, 1);
EDIT 2
Problem solved again.
I of course had to remove the margin setting of the textblock!
Thank you very much!
Greetings,
FunkyPeanut
Could you post the exception?
clearly there is an error in your code here:
WeekGrid.Children.Add(tmpTextBlock);
Grid.SetRow(tmpTextBlock, 1);
//add the border - these lines produce the problem
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
The component Border it is like a "container", which accepts a single element, you should switch to:
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
WeekGrid.Children.Add(border);
Grid.SetRow(border, 1);
You should also check if the resources are "available" for your access.
Related
I want to bring a some rectangles to my WPF-Pages, these Rectangles should have rounded corners. To bring a few of the rectangles to the page without having to write every single one in xaml I decided to do it with a loop in the code.
I tried this one:
for (int i = 0; i < 5; i++)
{
Rectangle rect = new Rectangle();
rect.Fill = System.Windows.Media.Brushes.Green;
var style = new Style(typeof(Border));
style.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(12.0, 0, 0 , 0)));
rect.Resources.Add(typeof(Border), style);
Grid.SetColumn(rect, 1);
Grid.SetRow(rect, 1);
mainGrid.Children.Add(rect);
}
but the corner radius of my rectangles won´t change. Do you have any suggestion?
Thanks for your help in advance!
To bring a few of the rectangles to the page without having to write every single one in xaml
Good problem to solve.
I decided to do it with a loop in the code
Absolutely bad solution. Use proper MVVM with an <ItemsControl> bound to your list of objects you're trying to display, stored in your view model. And then create a global style sheet and apply it to this either automatically or manually.
Anyway to answer your question, you're creating an unnamed style on Border and applying it to a Rectangle. That will never auto-apply, and good thing, because you reference Border.CornerRadiusProperty which doesn't exist on a Rectangle.
You want to either make your style override the Rectangle's template and add a Border around it, then set its corner border radius, or manually add the border above the rectangle and set its corner radius in your setter (only add the style to the Border's resources).
Your code doesn't really make sense to me though, Rectangle also has corner radius properties, RadiusX and RadiusY, you could just set those if that's what you want.
The rectangle is overflowing. If you do the same thing with a border it will work. When you add the rectangle inside the border you can see what its doing
Rectangle rect = new Rectangle();
rect.Fill = System.Windows.Media.Brushes.Green;
Border b = new Border();
b.Width = 100;
b.Height = 100;
b.Background = Brushes.White;
b.CornerRadius= new CornerRadius(12, 0, 0, 0);
b.BorderThickness = new Thickness(2);
b.BorderBrush = Brushes.Red;
b.Child = rect;//adding this rectangle will show you how the corner is overflowing
grid_Main.Children.Add(b);
I've a problem in WPF. I'm making a delete button with an image in it. When the button is disabled, however, I wanted to display a greyscale image.
I found the Thomas Lebrun implementation, but I don't want to add the whole class in my program. Instead I tried to mimic the behavior in this way:
BitmapImage img_Delete = new System.Windows.Media.Imaging.BitmapImage(new Uri("the png URI", UriKind.RelativeOrAbsolute));
ImageSource img_DeleteDisabled = null;
[...]
Button btDel = new Button() { Width = 20, Height = 20, ToolTip = "Delete", Margin = new Thickness(5), HorizontalAlignment = System.Windows.HorizontalAlignment.Right };
btDel.IsEnabled = isdeletable(obj);
if (!btDel.IsEnabled && (img_DeleteDisabled == null))
{
img_DeleteDisabled = new FormatConvertedBitmap(img_Delete, PixelFormats.Gray32Float, null, 0);
}
btDel.Content = (new System.Windows.Controls.Image()
{
Width = 16,
Height = 16,
HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
Source = btDel.IsEnabled ? img_Delete : img_DeleteDisabled
});
It behaves in the expected way, except.. Well, I'll show you:
The left one is enabled, the right one is disabled
As you can see the alpha channel is gone. How can I integrate it back?
Helped by the comments, I figured out I was thinking about the wrong place where to apply the opacity mask.
The OpacityMask should be applied to the button, not to the image. This is because the opacity applies to the whole image rather than its source.
For this reason, the correct way to implement this is
btDel.Content = (new System.Windows.Controls.Image()
{
Width = 16,
Height = 16,
HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
Source = btDel.IsEnabled ? img_Delete : img_DeleteDisabled,
OpacityMask = new ImageBrush(img_Delete)
});
This way the mask is applied to the button image. The result is what I needed:
I need to return width of GridComboBoxColumn based on the maximum length of combotext mentioned below, which is loaded as its ItemsSource . The maximum combotext is "Frankfurt a.M.-Germany-Country" and the width is calculted in WinRT and WPF as follows:
//Calculate width for maximum display text ComboBox items
TextBlock TextBlock = new TextBlock()
{
FontFamily = FontFamily,
Margin = Margin,
FontSize = FontSize,
TextWrapping=TextWrapping.NoWrap
};
//Set the TextBlock display text to combo items text and initialize Height and Width for TextBlock
TextBlock.Text = DisplayText;
var parentBorder = new Border { Child = TextBlock };
TextBlock.MaxHeight = size.Height;
TextBlock.MaxWidth = double.MaxValue;
this.DataGrid.UpdateLayout();
//Measuer the width and Height of parentBorder
parentBorder.Measure(new Size(TextBlock.MaxWidth, TextBlock.MaxHeight));
parentBorder.Child = null;
return parentBorder.DesiredSize.Width;
In WPF, the width returned as 182.46 and in WinRT the width is returned as 248 . But in WinRT the column width fit with ComboBox, in WPF the ComboBox does not fit inside the column width. Please advise how the size is measured in both WPF and WinRT?
In my Windows Phone 7 app, I would like to have a TextBlock followed by a CheckBox. From left to right.
I am doing this programmatically, and I can do this with the following code:
StackPanel ControlStackPanel = new StackPanel();
ControlStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
TextBlock ControlTextBlock = new TextBlock();
ControlTextBlock.Text = #"ControlNameGoesHere";
CheckBox ControlCheckBox = new CheckBox();
ControlCheckBox.Margin = new Thickness(0, 0, 0, 0);
ControlCheckBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
ControlStackPanel.Children.Add(ControlTextBlock);
ControlStackPanel.Children.Add(ControlCheckBox);
However, I would actually like the TextBlock element to align to the left side of the screen, and the CheckBox element align to the right side of the screen. How can I do this?
Do I need to add a grid programmatically? The reason I don't want to do this in XAML is that adding these stackpanels is going to be an iterative process, and it just works better with my code if I do it all programmatically.
I've googled a heck of a lot, but haven't had much luck.
Many thanks!
Brett
A content control only has one alignment at once. All the controls within it are aligned according to it's horizontal alignment. You may wish to have two content controls with the different alignments inside a larger outer container.
Grid
StackPanel left
Control
StackPanel right
Control
Try to place your controls into columns of Grid. ControlTextBox should go to the first column of the Grid, ControlCheckBox - into the second one.
Here's some code:
TextBlock ControlTextBlock = new TextBlock();
ControlTextBlock.Text = #"ControlNameGoesHere";
ControlTextBlock.SetValue(Grid.ColumnProperty, 0);
ControlTextBlock.VerticalAlignment = System.Windows.VerticalAlignment.Top;
CheckBox ControlCheckBox = new CheckBox();
ControlCheckBox.Margin = new Thickness(0, 0, 0, 0);
ControlCheckBox.SetValue(Grid.ColumnProperty, 1);
ControlCheckBox.VerticalAlignment = System.Windows.VerticalAlignment.Top;
ColumnDefinition FirstColumn = new ColumnDefinition();
FirstColumn.Width = System.Windows.GridLength.Auto;
ColumnDefinition SecondColumn = new ColumnDefinition();
SecondColumn.Width = System.Windows.GridLength.Auto;
Grid ContentGrid = new Grid();
ContentGrid.ColumnDefinitions.Add(FirstColumn);
ContentGrid.ColumnDefinitions.Add(SecondColumn);
ContentGrid.Children.Add(ControlTextBlock);
ContentGrid.Children.Add(ControlCheckBox);
I am using drag and drop functionality in an application and I need to change the look of a Grid based on the location of the drag point.
For example, I would like to be able to call something like the following that would change the border to only show the bottom, or the top,etc. This example would be that when there is a drag operation performed over Grid, the top border of the grid would be the only one set to a thickness of 5 and would be black.
private void Grid_DragOver(object sender,DragEventArgs e)
{
Grid grid = (Grid)sender;
Border border = new Border();
border.BorderBrush = Brushes.Black;
border.BorderThickness = new Thickness(0,5,0,0);
border.Child = grid;
}
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.margin.aspx
Edit: Border color http://msdn.microsoft.com/en-us/library/system.windows.controls.border.borderbrush.aspx