I'm successfully getting a TextBox to resize with a parent Border height but I need the TextBox to actually be 50 pixels smaller in height than the parent Border.
Any ideas how to achieve this?
The code I'm using is
<Border VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<TextBox x:Name="txtActivityNotes" HorizontalAlignment="Stretch" Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}, Path=ActualHeight}" AcceptsReturn="True" VerticalContentAlignment="Top" TextWrapping="WrapWithOverflow" VerticalScrollBarVisibility="Auto" />
</Border>
Can't you just set a bottom margin of 50?
<TextBox Margin="0,0,0,50" />
I've tried following and it works.
add following to text box in xaml:
------
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Height="{Binding RelativeSource={RelativeSource FindAncestor, *AncestorType*={x:Type *Grid*}}, Path=ActualHeight}"
------
Here, AncestorType is type of container containing the textBox. In my case it was 'Grid'.
And also add margin like,
Margin="0,0,0,50"
to keep distance from border below.
============
oops sorry i posted on the same page.!
how about using a converter on the binding to minus 50 off the height
heres an example of using a converter
Related
I'm developping with VS 2015 and WPF a mask designer, in which i can drag, move and resize controls.
One of These controls is a Label which has a TextBlock as Content.
This looks in my XAMl like that:
<Label Background="AliceBlue" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top">
<TextBlock TextWrapping="Wrap" Height="Auto">Label</TextBlock>
</Label>
For the TextBlock i have set TextWrapping to "Wrap" and Height to "Auto".
When i try to resize the height of the Label at it's allowed minimum, the content of the TextBlock shall still be completely visible.
I tried this with a TextBlock and it worked.
But when i try it with a Label implementing a TextBlock it doesn't work, the content of the TextBlock isn't completely visible anymore.
How can i synchronize the parent's height to the child's height?
Thanks in advance!
Patrick
You can use the Name of parent and bind its ActualHeight
<Label x:Name="parentElementName" Background="AliceBlue" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top">
<TextBlock TextWrapping="Wrap" Height="{Binding ActualHeight, ElementName=parentElementName}">Label</TextBlock>
</Label>
Edit
<Label x:Name="parentElementName" Background="AliceBlue" Height="{Binding ActualHeight, ElementName=Child}"HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top">
<TextBlock x:Name="Child" TextWrapping="Wrap" Height="Auto">Label</TextBlock>
</Label>
#Ghassen's answer is basically correct. If you want to bind the parent to the child then reverse the answer above. The reversed code is shown below.
<Label x:Name="ParentLabel" Height="{Binding ElementName=ChildBlock, Path=ActualHeight}" Width="{Binding ElementName=ChildBlock, Path=ActualWidth}" Background="Blue">
<TextBlock x:Name="ChildBlock" Height="100" Width="100" Background="Green"></TextBlock>
</Label>
In the following XAML, I am placing an InkCanvas on top of a RichTextBox control. In MVVM style, is there any way of returning the underlying RichTextBox object with the Mouse Events? (This so as to acquire the mouse position and closest word within the RichTextBox).
Thanks for any help or ideas.
<UserControl x:Class="Nova5.UI.Views.Ink.InkRichTextView"
.....
<Grid>
<f:SimpleRichTextBox Name="simplerichtextbox"
RichText="{Binding RichText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
>
<FlowDocument PageWidth="{Binding ElementName=simplerichtextbox, Path=ActualWidth}" />
</fsc:SimpleRichTextBox>
<InkCanvas
Height="{Binding ElementName=simplerichtextbox, Path=ActualHeight}"
Width="{Binding ElementName=simplerichtextbox, Path=ActualWidth}"
Background="Transparent"
DefaultDrawingAttributes="{Binding Pen}"
EditingMode="{Binding EditingMode}"
Strokes="{Binding Strokes}"
h:MouseBehaviour.MouseUpCommand="{Binding MouseUpCommand}"
h:MouseBehaviour.PreviewMouseDownCommand="{Binding PreviewMouseDownCommand}"/>
</Grid>
</UserControl>
i do have the following structure:
<UniformGrid HorizontalAlignment="Stretch" Grid.Row="0" VerticalAlignment="Top" Columns="6" DockPanel.Dock="Right" >
<StackPanel HorizontalAlignment="Stretch">
<DockPanel Background="#FF393939" >
<Label Content="{lex:Loc Site}" Foreground="#FFE0E0E0"/>
</DockPanel>
<ListBox Height="300" ItemsSource="{Binding Sites.View}" DisplayMemberPath="Name.ActualTranslation">
</ListBox>
</StackPanel>...
These stackpanels in my UniformGrid should be spreaded over the whole MainWindow (or View)... Still if there is an item in a Listbox which has a longer string which needs more place than the standard width it autofits the string and i have to scroll vertically.
I dont want the listview to gain the width if the content has not enough place.
I also have a scrollviewer in my MainWindow in which this view is placed...
What can i do that the uniform grid stays the same width like the MainWindow-...
I've already tried to give a name to the MainWindow and Bind the width to something like this: Width="{Binding ElementName=MainWindow, Path=ActualWdith}" ..
though i get the error:
Cannot find source for binding with reference 'ElementName=MainWindow'. BindingExpression:Path=ActualWdith; DataItem=null;
What can i do to fix this issue?
If you need more information or my error description isnt clear dont hesitate to ask.
UPDATE:
how it should be:
behavior when the text is too long:
and a scrollbar appears on the bottom..
If you are trying to access a WPF object by name it needs to have x:Key="name" which wasn't shown in your code above, i.e.
<Window x:Name="MainWindow" ....
Alternatively you can walk back up the tree until you find an object of the correct type,
Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MainWindow}, Path=ActualWidth}"
Alternatively you can simply change the way you show the text to constrain its width
<ListBox Height="300" ItemsSource="{Binding Sites.View}" DisplayMemberPath="Name.ActualTranslation">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" TextWrapping="Wrap" MaxWidth="200"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have an ItemsControl presenting a list of buttons. Each button has an image as it's content (png), but the image looks slightly different for each row.
The below image is magnified version of what I'm seeing:
Here is the xaml:
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="tb1">hello</TextBlock>
<Button Height="{Binding ElementName=tb1, Path=ActualHeight}" Padding="0,-3,-3,-3" BorderBrush="Transparent" Background="Transparent" >
<Image Stretch="Fill" Source="stock_standard_filter.png" Margin="0">
</Image>
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I've had a similar issue previously and was able to solve it using SnapsToDevicePixels="True", but that solution is not working this time. I've also tried UseLayoutRounding="True" and RenderOptions.EdgeMode="Aliased"
The height of the button is bound so the image will be stretched to fill the button. Because WPF uses doubles (1/96 inch units) there is bound to be some rounding off. SnapsToDevicePixels and Layout rounding might help when you use them on the StackPanel but as long as you stretch the image it will get blurred.
My best guess is to set "Stretch to None" and experiment with SnapsToDevicePixels and Layout rounding.
I am kinda new to WPF, dont know if the question is weird. I wanted to know if its possible to add a border around the text inside a textBlock.
EDIT:
As per suggestion I have tried both but without much success:
<Border BorderBrush="#FF0B232F" BorderThickness="2">
<TextBlock HorizontalAlignment="Left" Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black"/>
</Border>
and
<Label BorderBrush="#FF0B232F" BorderThickness="2,2,2,2" Content="TextBlock" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" />
Am I doing something wrong here?
In such cases I use Label or TextBlock placed in Border.
Both your approaches are correct, however, if you have the textblock/label inside a grid (or any other container) declared as you have, its contents will stretch.
Try setting the VerticalAlignment and/or HorizontalAlignment to an appropriate setting (Top/Left, Center)..
something like this:
<Border BorderBrush="#FF0B232F" BorderThickness="2" VerticalAlignment="Top">
<TextBlock HorizontalAlignment="Left" Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black"/>
</Border>
Assuming that you are asking for a full size TextBlock with a border overlay within the bounds of the TextBlock you could wrap it in a Grid and draw the borders over the top of the TextBlock like this...
<Grid HorizontalAlignment="Left">
<TextBlock Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black" />
<Border BorderBrush="#FF0B232F" BorderThickness="2" />
</Grid>
Because we haven't specified the grid row and column on the TextBlock and Border objects the border overlays on top of the TextBlock.
if you just want to have a border around your textblock or any other control use :
<Border>
<TextBlock></TextBlock>
</Border>
you set border properties like color ,cornerradius ,thickness,...