Hey. Another WPF question. In my XAML code I have a border:
<Border x:Name="myBorder" Background="AliceBlue"
Width="200" Height="200"
BorderThickness="10" BorderBrush="Black">
</Border>
and somewhere in code I increase the BorderThickness
double thickness = myBorder.BorderThickness.Bottom + 2;
myBorder.BorderThickness = new Thickness(thickness);
and the result is that the border's weight increases but not outside the 200x200 width-height, but inner, decreasing the dimension. Is there a way to do the opposite?
Well, actually you should set the width and height on the inner or outer control of the border, not on the border itself. Then you can set a negative margin for the border, equal to minus the value of the border thickness. Something like this should to the trick:
<Border x:Name="myBorder" Background="AliceBlue"
Margin="-10,-10,-10,-10" BorderThickness="10" BorderBrush="Black">
<Button Background="Red" Content="Test" Width="200" Height="200"></Button>
</Border>
It looks like you need to increase Width and Height accordingly.
Related
I'm trying to place a Border around a StackPanel like so:
<Border CornerRadius="5" BorderBrush="#009999" Background="Transparent"
BorderThickness="2" Grid.Column="2" Grid.Row="0">
<StackPanel HorizontalAlignment="Center" Margin="0,30,0,0">
<!--Contents-->
</StackPanel>
</Border>
For some reason, the border is appearing around the column that the StackPanel is in, not the StackPanel itself. I can change the margin of the border, but that doesn't hold up if the screen is resized. I also tried getting rid of the StackPanel margin but that made no difference. The StackPanel is the only object in this column.
Border has default HorizontalAlignment equal to Stretch, so it takes full possible width.
Then StackPanel is centered inside the border because it has HorizontalAlignment="Center".
set HorizontalAlignment="Center" on Border. It will get width, equal to StackPanel width (plus Margin), and then will be centered
I think it is because you're setting the HorizontalAlignment to Center. Doing this will make the StackPanel size to whatever its content is.
If you move the Border to stackpanel your problems will be fixed :)
here it is:
<Grid>
<StackPanel Width="350" Height="200">
<Border BorderThickness="2" BorderBrush="Black" Width="350" Height="200">
<TextBlock>SomeText</TextBlock>
</Border>
</StackPanel>
</Grid>
I have canvas with user control (border with textbox). when I put sth I want to resize textbox with border from left to right side. I have event textBox_TextChanged, and there I set new border width from textbox width.
please look at image
Do you want the border+textbox to expand to the left as you type? Try to set the Canvas.Right to position your Border:
<Canvas Width="300" Height="200">
<Border Canvas.Top="10"
Canvas.Right="50"
BorderBrush="Orange"
BorderThickness="2">
<TextBox Text="{Binding Title}" />
</Border>
</Canvas>
Obviously, Title above is the viewmodel's property.
I would like to create a custom border around a content, that the upper and lower side of the border is not continuous, it should have a style like bracket as below. (inside the "bracket border" the content should be placed e.g. grid, stackpanel, etc.)
Note that the height of the right and the left border can be changed depending on the content's height, whereas the top and the bottom should have a standard width.
[ sample content ]
In order to achieve this, I separated the view in a 3 columns grid:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="22px"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="22px"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="Blue" BorderThickness="6px 6px 0px 6px"></Border>
<Border Grid.Column="1" BorderBrush="Transparent" BorderThickness="6px">
<!--Place for the actual content of the border-->
<TextBlock Text="Test" FontSize="15"></TextBlock>
</Border>
<Border Grid.Column="2" BorderBrush="Blue" BorderThickness="0px 6px 6px 6px"></Border>
</Grid>
Is there another approach for achieving this style?
One possible solution is to write your own Border based on a Decorator.
An implementation (for a different border) can be found in How can I draw a border with squared corners in wpf?
One simple trick is try setting some LinearGradientBrush for the BorderBrush. If your text has a fixed width, it will look best at all time. However when the text's width may change, the horizontal lines (at 2 ends) will shrink/extend at some ratio. That's because we set some Offset for the GradientStop and it's pity that this Offset can only be set based on some ratio (from 0 to 1) with the width of the whole Brush (which is exactly the width of the Border when the background is stretched). Note that the MappingMode cannot change this behavior, it just works for StartPoint and EndPoint.
Here is the pure XAML code:
<Border VerticalAlignment="Center" HorizontalAlignment="Center"
BorderThickness="3" Padding="5,0,5,0">
<TextBlock Text="Square bracket border here" FontSize="30"
HorizontalAlignment="Center"/>
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0,1" EndPoint="1,1">
<GradientStop Offset="0.03" Color="Blue"/>
<GradientStop Offset="0.03" Color="Transparent"/>
<GradientStop Offset="0.97" Color="Transparent"/>
<GradientStop Offset="0.97" Color="Blue"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
You can change the Offset of the first 2 GradientStops to what you want, the Offset of the remaining GradentStops should be the subtraction of the first's Offset from 1.
If using some code behind, you can pinpoint exactly the length of the horizontal lines (at the 2 ends). That way we need some Binding between the Offset and the ActualWidth of the Border. Next we need some Converter here, this Converter will convert the ActualWidth and the desired exact length to the correct ratio. So when the text width changes, the length of the horizontal lines will always be some fixed value.
You can try the following XAML code:
<Grid>
<Border BorderBrush="Black" BorderThickness="1"/>
<TextBlock Text="sample content"/>
<Border BorderBrush="White" BorderThickness="0,1,0,1" Margin="8,0,8,0"/>
</Grid>
Second border's color "White" can be replaced with the actual background color. "Transparent" color will not help.
Thanks,
RDV
I'd like to display a content enclosed by multiple borders of different colors and with rounded corners. I experimented with corner radius but could never get perfect corners (i.e. adjacent to corners of a parent border) on nested borders. What is the recommended practice in this case, if any?
The following is what I tried:
<Border Grid.Row="1" Grid.Column="1" CornerRadius="3" BorderThickness="3" BorderBrush="#FAFAFA"Background="#FAFAFA" Margin="40,40,40,40" Padding="0">
<Border.Effect>
<DropShadowEffect Color="#1f40618c" />
</Border.Effect>
<StackPanel>
<Border CornerRadius="3" BorderThickness="3" BorderBrush="#696B6D" Background="#696B6D" Margin="0" Padding="0" >
<Border CornerRadius="2.3" BorderThickness="3" BorderBrush="Black" Margin="0" Padding="32,32,32,16" Background="#102F8C">
<Grid />
</Border>
</Border>
</StackPanel>
</Border>
Thanks.
You have to take into account that the BorderThickness adds to the total width or height of a Border control. You can think of it as if half of the border line lies inside the control and half lies outside. On the other hand the CornerRadius is given relative to the center of the border line.
Consequently the difference between the inner and outer radius should be equal to the sum of half of both BorderThicknesses (plus perhaps the outer Padding and the inner Margin).
With your nested borders that have a BorderThickness of 3 each and an outer CornerRadius of also 3 that would result in an inner CornerRadius of 0.
You may simply set the inner CornerRadius to some sensible value and add 3 to that value for the outer one:
<Border BorderBrush="Blue" BorderThickness="3" CornerRadius="6">
<Border BorderBrush="Red" BorderThickness="3" CornerRadius="3">
</Border>
</Border>
is it possible that if i put a textbox in border container and will not specify height & width of textbox rather specify height & width for border and textbox will take the size of the border. how could i do this. i tried to do it in xaml but fail. need answer. thanks
http://msdn.microsoft.com/en-us/library/system.windows.horizontalalignment.stretch.aspx
It's nice and easy:
<Border Width="200" Height="70" BorderBrush="Black" BorderThickness="2">
<TextBox></TextBox>
</Border>
<Grid>
<Border Width="100" Height="100">
<TextBox />
</Border>
</Grid>
The text box will be of size of the border.