WPF resizing TextBlock - wpf

I have a textblock in a grid in WPF.
I want the text to dynamically size (font) when resized. At the moment textboxes, comboboxes do this, but the the textblock stays the same. Is it possible?

Instead of trying to manually rig a TextBlock to do this, just edit the default template of a TextBox and remove the border and background, then in the style make it set the IsReadOnly flag. This way you get the textblock sizing, as well as copy-paste for free.

You can use a ViewBox for this.
eg:
<Viewbox Stretch="Uniform">
<TextBlock Text="Test" />
</Viewbox>

use expression blend for getting the default template of TextBox, then you can edit it as you required.

Related

WPF borders and the controls within them

This post is about the controls contained within a WPF Border control. It's also about having a border that can appear and disappear without affecting the contained controls.
For the record, I'm using C# and WPF and most of the view stuff is using XAML. I also use MVVM although I'm not sure that's going to be related.
What I had planned for was a border around a control that I could make appear and disappear, for the effect of a highlight or something like that. But when I change certain properties of the Border, for example the Opacity or Visiblity, they impact on the contained controls. I have also tried changing the Background property to Transparent and that has not made a difference.
I do know that some controls have a Border property, but that's not really the case for my situation.
How can I do this?
Thanks
Try this:
<Grid>
<Border BorderThickness="2">
<YourControl />
</Border>
<Border Opacity="0.5" BorderBrush="Red" BorderThickness="2" />
</Grid>
This way you can change the opacity of the second border without affecting your control. The trick is that Grid ensures that both elements inside it have the same dimensions.
Also notice how your control is wrapped in another border with the same thickness but with no brush. This is to keep the second border from obscuring your control.

Disable extra row in listview wpf

I have this ListView and at the end of the last item you can click to the right and type in a TextBox, like shown. How can I disable this?
If you want to prohibit writing in TextBox of your DataTemplate of ListView, then just set 'IsReadOnly' property of TextBox to true. Let me show an example:
<TextBox IsReadOnly="True" />
Or just use TextBlock instead of TextBox. TextBlock, by default, does not give an opportunity to write inside this control:
<TextBlock Text="I am a TextBlock. You cannot change this text.:)"/>

Make a silverlight textblock act like a hyperlink

I'm pretty new to silverlight. I have a text block that is displayed inside a datagrid
(inside a DataGridTemplateColumn.CellTemplate template to be precise).
I'd like to dynamically make some of the textblocks into hyperlinks that open a new window.
Is there a way to do this - so far all I can come up with is using a hyperlink button and trying to style it to look like a text block.
Any help would be very much appreciated.
HyperlinkButton is a ContentControl so it can actually take some kind of pre-styled TextBlock (or other control) as it's content (instead of just using a simple string as Content).
<HyperlinkButton NavigateUri="http://myurl.com">
<TextBlock Text="My Link Text" Foreground="Black" />
</HyperlinkButton>
You would have to use a custom HyperlinkButton template if you wanted to style it to get rid of the default teal colored focus ring, etc. You could also set the IsEnabled property of the HyperlinkButton to false to prevent link behavior on any cells that weren't actually links if you are trying to set them up in some dynamic way.

Silverlight button style

I am trying out new styles with silverlight, and I created a new button skin with blend that consists of a border and a textblock. Wondered if there is a way to change the the text of the textblock when the the button's content(text) property is changed.
The binding would look like this:
<TextBlock Text="{TemplateBinding Content}"/>
The problem is when I try to set the content to something other than text:
<Button>
<Button.Content>
<Rectangle Fill="#FFB51111"/>
</Button.Content>
</Button>
In this case using the ContentPresenter would work better. It uses the same binding expression, but can display more than text. But all that is really up to you.
I don't really get what your trying to do. Normally, you include a TextBlock like that as a part of the button content.
Use a ContentPresenter rather than a TextBlock in your Template.

How do I make an item in a toolbar fill all available space in WPF

I'm trying to make an item on ToolBar (specifically a Label, TextBlock, or a TextBox) That will fill all available horizontal space. I've gotten the ToolBar itself to stretch out by taking it out of its ToolBarTray, but I can't figure out how to make items stretch.
I tried setting Width to Percenatage or Star values, but it doesn't accept that. Setting Horizontal(Content)Alignment to Stretch in various places seems to have no effect either.
Unfortunately it looks like the default ControlTemplate for ToolBar doesn't use an ItemsPresenter, it uses a ToolBarPanel, so setting ToolBar.ItemsPanel won't have any effect.
ToolBarPanel inherits from StackPanel. By default its Orientation is bound to the parent ToolBar.Orientation, but you can override this and set its Orientation to Vertical with a Style and this will allow items to stretch horizontally:
<DockPanel>
<ToolBar DockPanel.Dock="Top">
<ToolBar.Resources>
<Style TargetType="{x:Type ToolBarPanel}">
<Setter Property="Orientation" Value="Vertical"/>
</Style>
</ToolBar.Resources>
<ComboBox HorizontalAlignment="Stretch" SelectedIndex="0">
<ComboBoxItem>A B C</ComboBoxItem>
<ComboBoxItem>1 2 3</ComboBoxItem>
<ComboBoxItem>Do Re Mi</ComboBoxItem>
</ComboBox>
</ToolBar>
<Border Margin="10" BorderBrush="Yellow" BorderThickness="3"/>
</DockPanel>
You can then use a Grid or something in place of the ComboBox above if you want multiple items on a line.
Try putting a horizontal StackPanel in the ToolBar and then the element you want inside of that StackPanel.
Have you tried wrapping your item in a Grid, not in a StackPanel?
You need a special custom panel like auto stretched stack panel, and replace the toolbarpanel. Check this out you can find one panel there
http://blendables.com/products/productsLayoutmix.aspx
I've had this same problem for a while, and there is very little help available online.
Since it doesn't sound like you need all the extra functionality of a Toolbar (collapsible/movable trays), why not just use a Top-docked Grid, and tweak the background a little to make it look like a standard toolbar?

Resources