How to select text in silverlight 3.0 text block - silverlight

Is it possible to allow a user to select text in a silverlight text block (not text box) as they would be able to in any HTML page?

I later found a solution, and I wanted to share it. The solution can be found here.
Excerpt from that page:
...change the textbox's style. Put the following Xaml code in App.xaml or some other resource:
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid x:Name="RootElement">
<ScrollViewer x:Name="ContentElement" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" BorderThickness="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then set your textbox's style as "{StaticResource TextBoxStyle}", and set IsReadOnly property as true, your textbox will look like a textblock but it can be copied.

No. The Silverlight TextBlock doesn't support selection. You would need to use a TextBox in read-only mode instead. To make the user experience a bit more seamless, you could style the TextBox to have a normal arrow cursor instead of an I-beam.

Related

Why is my XAML control losing properties when applying a style template to it?

I'm trying to style this button, and I even though I state the height it should be in my XAML, the template in the styling seems to get rid of it.
Note: I am aware I can just have a style with no template, but I need the template because I'm using multiple themes / style files.
Control:
<Button Style="{DynamicResource basicButton}"
Height="50">
<Canvas...>
<Path... />
</Canvas>
</Button>
Styling:
<Style x:Key="basicButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{DynamicResource solid_single_mainColor}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Is this expected behavior? I intent to re-use this button style, so I need to be able to give it a height in the XAML. What am I missing? How can I give each new button I create a different height?
The problem is likely coming from the Canvas that you are placing inside the Button. In many cases a Canvas will have unbounded size unless you explicitly clip it. The Height of the Button itself isn't affected by its template as layout measurement will use the explicit value you have set but you may not be able to tell visually what size the Button itself is.

Disable whole user control focusing from keyboard

I have UserControl in a window. When user walks window with "Tab" key user control gets focused and dashed border drawn around it. How to prevent this behavior?
Try it for an control set Focusable = "False". Example:
<Grid Focusable="False">
...
</Grid>
Or set the Style to focus yourself:
<Grid FocusVisualStyle="{x:Null}" />
Also, the Style of focus might be:
<Style x:Key="MyItemFocusVisual" TargetType="{x:Type Control}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Border SnapsToDevicePixels="True" CornerRadius="0" BorderThickness="5" BorderBrush="#7B2F81" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Using:
<Grid Focusable="True" FocusVisualStyle="{StaticResource MyItemFocusVisual}" ... />
Output
If you just want to keep it from accepting focus via Tabbing just declare it on the object via IsTabStop="False" or you can edit the control Template for it and get rid of the Focus changes.
It was my mistake. I had xaml:
<ContentControl>
<ScrollViewer name="viewport"/>
</ContentControl>
and "viewport.Content" was set to my UserControl from code-behind.
It was a ContentControl who draw the focus border. I removed it and left only a . Problem solved.

Style AutoCompleteBox Error Indicator

Is it possible to change the style of the red rectangle that appear when the AutoCompleteBox from the WPF Toolkit has an error? I successfully changed it on the TextBox just creating a new style for the control but no matter what I do with the AutoCompleteBox I can't get rid of that red rectangle. Even if I remove the style like this:
<input:AutocompleteBox Style="{x:Null}"/>
I cannot see the control but if I have an error on the control binding, it still shows a red line! What I would really like is that the AutoCompleteBox use the internal TextBox validation indicator but first I need to remove that outer rectangle. Any ideas on how to do this or what is creating that red rectangle?
AutocompleteBox consists of TextBox, SelectionAdapter, DropDownToggle and Popup.
And error-sate style, that you've described, is defined in TextBoxStyle:
<Style TargetType="controls:AutoCompleteBox">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:AutoCompleteBox">
<Grid Margin="{TemplateBinding Padding}"
Background="{TemplateBinding Background}">
<TextBox IsTabStop="True" x:Name="Text" Style="{TemplateBinding TextBoxStyle}" Margin="0" />
...
To change TextBox behavior when error - just change it's style.

Setting TextTrimming (CharacterEllipsis) in DataGrid's Cell

I would like to apply TextTrimming property (CharacterEllipsis) to the text in WPF DataGrid cells.
I applied custom DataGridCell template as in this answer (code below) and it works well, except for the Hyperlink columns like the first one in the picture), which now are empty.
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Padding="3" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis" Text="{Binding Text}"/>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I can see the difference in both column types in visual tree:
but don't understand how I can use this information to apply TextTrimming to TextBlock's columns of both type. Thanks for your time ;)
I finally ended up with the following solution (more like a workaround, but it works fine):
1) I assigned an x:Key to the style in question and applied it as a CellStyle to all DataGridTextColumns that should have their contents trimmed and ellipsisized whenever they don't fit
2) To apply ellipsis trimming in DataGridHyperlinkColumns, in App.xaml I added the following style:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
</Style>
which will apply to all implicitly generated TextBlocks (as described in CodeNaked's answer). This might seem a bit overkill, but I can't see much difference in rendering performance and Hyperlinks are now trimmed as expected.

ItemContainerStyle in Custom Control Derived From ListBox

Please bear with me Silverlight Designer Gurus, this is compicated (to me).
I'm creating a custom control which derives form the Silverlight 3.0 ListBox. In an effort not to show tons of code (initially), let me describe the setup.
I have a class library containing a class for my control logic. Then I have a Themes/generic.xaml that holds the styling details. In generic.xaml, I have a style that defines the default layout and look for the ListBox where I'm setting a values for the Template, ItemsPanel and ItemTemplate.
In my test app, I add my control on to MainPage.xaml and run it and it works great. I dynamically bind data to my control and that works fine.
Now I want to set the ItemContainerStyle for my derived control. If I create a style in the MainPage.xaml file and set the ItemContainerStyle property to that control as in:
<dti:myControl x:Name="MyControl1" ItemContainerStyle="{StaticResource MyListBoxItem}"
Height="500"
Width="200"
Margin="10"
Background="AliceBlue"
/>
It works as expected.
However, I'd like to do this in the class library or, more specifically, in generic.xaml. I tried to this Setter to my current Style:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<ControlTemplate>
<Grid Background="Red" Margin="3">
<ContentPresenter x:Name="contentPresenter"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="Stretch" Margin="3"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
And it fails miserably with:
"System.ArgumentException: 'System.Windows.Controls.ControlTemplate' is not a valid value for property 'ItemContainerStyle'."
Note: This is not my actual style I'd like to use for ItemContainerStyle. I'm actually looking to plug in some VSM here for the various selected/unselected states of the a ListBoxItem (for a dynamically bound control).
So, to the question is how do I apply the ItemContainterStyle to my custom control when it's defined using generic.xaml? I do not want that property set when I actually use the control later on.
Thanks,
Beaudetious
You missed to put Style tag inside your Setter.Value. ItemContainerstyle explects a Style to ListBoxItem(Unless you subclassed ListBoxItem to your own derived version.)
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType=”{x:Type ListBoxItem}“ >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Background="Red" Margin="3">
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="Stretch" Margin="3"/>
</Grid>
</ControlTemplate>
<Setter.Value>
</Style>
</Setter.Value>

Resources