I'm applying this style to an infragistics control XamDateTimeEditor.
The new error icon displays as expected.
issue: The original error style is still displaying when a validation error occurs. Thus making the control to have two error icon styles.
<Style TargetType="{x:Type XamDateTimeEditor}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<DockPanel>
<Border
Background="Red"
Width="25"
Height="25"
CornerRadius="10"
>
<TextBlock
Text="Error"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontWeight="Bold"
Foreground="White"
/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Using a view model that implements IDataErrorInfo is the best approach when customizing an error template that targets XamDateTimeEditor.
This was previously discussed and demonstrated on our forums at: http://www.infragistics.com/community/forums/t/109152.aspx
https://codeblitz.wordpress.com/2009/05/08/wpf-validation-made-easy-with-idataerrorinfo/
Related
I've just compiled an old project that uses Presentation Framework's DataVisualization and I'm getting a "Not Suported Exception":
Cannot convert the value in attribute 'Property' to object of type 'System.Windows.DependencyProperty'.
at
System.Windows.Markup.DependencyPropertyConverter.ResolveProperty(IServiceProvider serviceProvider, String targetName, Object source)"
Great, easy to solve, just don't set a non dependency property in a setter right?
Except it's occurring on the code-behind line:
var la = new LinearAxis();
and the exception in the presentation framework itself, which I have no control over. Has anyone encountered this? How can I solve an error in an external library like this? I can't imagine it's a bug in the constructor, I must be doing something on my end that's causing it.
This is the chart style, I'll try removing it and see if that's affecting things:
<Style TargetType="{x:Type chartingToolkit:Chart}">
<!--<Setter Property="LegendStyle" Value="{StaticResource LedgendStyle}"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type chartingToolkit:Chart}">
<Border Background="{TemplateBinding Background}" BorderBrush="{x:Null}" BorderThickness="0" Padding="2">
<Grid>
<Primitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
<Grid Panel.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" Background="{StaticResource GraphBackground}" />
<Border Panel.ZIndex="10" BorderBrush="{StaticResource GraphBorder}" BorderThickness="{StaticResource GrephBorderThickness}" />
</Primitives:EdgePanel>
<visualizationToolkit:Legend
x:Name="Legend"
Style="{TemplateBinding LegendStyle}"
Grid.Column="1"
Margin="50,10"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Background="{StaticResource LegendBackground}"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
One thing I've noticed, if I run without debug everything seems fine...
I'm attempting to create a control template for the Silverlight RadioButton, using a ToggleButton for each item. The problem I'm running into is that the selection mechanism appears to be broken. Here's the (simplified) style I'm using:
<Style TargetType="RadioButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Grid>
<ToggleButton x:Name="toggle"
IsChecked="{TemplateBinding IsChecked}">
<ContentPresenter x:Name="contentPresenter" />
</ToggleButton>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The test is simply whether the radio selection actually works -- so for example, the text blocks below don't show the correct value when you click on the radio buttons:
<StackPanel>
<RadioButton x:Name="radio1" GroupName="Test" Content="1" />
<RadioButton x:Name="radio2" GroupName="Test" Content="2" />
<TextBlock Text="{Binding ElementName=radio1,Path=IsChecked,StringFormat='Radio 1 checked: {0}'}" />
<TextBlock Text="{Binding ElementName=radio2,Path=IsChecked,StringFormat='Radio 2 checked: {0}'}" />
</StackPanel>
You would think that maybe there is a named part in the control template, which the control uses to update the selection -- however, the docs indicate no named parts. So what is going on here, and how can I get my example working?
You would think that maybe there is a named part in the control template
Yes ... it turns out there is an "undocumented feature". Trial and error shows that the "BoxMiddleLine" is the named part that controls the selection. So adding to the control template a clickable element with that name, fixes the issue:
<Grid>
<Border x:Name="BoxMiddleLine" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#0000" />
<ToggleButton x:Name="toggle"
IsChecked="{TemplateBinding IsChecked}"
IsHitTestVisible="False">
<ContentPresenter x:Name="contentPresenter" />
</ToggleButton>
</Grid>
i want to show a focus image around a text box when it got focus. so i create following style
<Style x:Key="TextBoxFocusVisualStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Image Source="/WPFApp;component/Resources/txtFocus.png" Stretch="Fill" Margin="-8,-6,-8,-6"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and in window xaml file i used this style as following
<TextBox Grid.Column="1" Height="34" Margin="186,48,0,0" Name="txtEmailId" VerticalAlignment="Top" KeyboardNavigation.TabIndex="0" MaxWidth="293" HorizontalAlignment="Left" Width="293" Text="" FocusVisualStyle="{DynamicResource TextBoxFocusVisualStyle}"/>
but problem is that it does not work during loading. When window load then initially focus is on that textbox and at that time it does not show the image .However when i navigate to other textbox (and other control) then it show focus image. and finally when i focus return to that textbox then it display the focus image
so problem is that it does not show focus image first time on when window loaded. Please suggest that where i am wrong.
Consider that FocusVisualStyle applies to a control only when focused by keyboard (TAB key).
This is different from the logical focus that you obtain for example using
Control.SetFocus()
For an overview on Focus have a look at
http://msdn.microsoft.com/en-us/library/aa969768.aspx
A possible workaround for your problem is work with DependencyProperty IsFocused an use Style instead of FocusVisualStyle
<Style x:Key="TextBoxStyle" TargetType="{x:Type Control}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Image Stretch="Fill" Margin="-8,-6,-8,-6" Source="/WPFApp;component/Resources/txtFocus.png" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
And then in the main Window
<TextBox Grid.Column="1" Height="34" Margin="186,48,0,0" Name="txtEmailId"
VerticalAlignment="Top" KeyboardNavigation.TabIndex="0" MaxWidth="293"
HorizontalAlignment="Left" Width="293" Text=""
Style="{DynamicResource TextBoxFocusVisualStyle}" Background="White" />
Hope this heps
Based on the following code :
<GroupBox>
<GroupBox.Template>
<ControlTemplate TargetType="{x:Type GroupBox}">
<ContentPresenter TextElement.FontSize="28" />
</ControlTemplate>
</GroupBox.Template>
<TextBlock>Test</TextBlock>
</GroupBox>
I was expecting "Test" to be displayed with FontSize=28. But it uses the default size instead.
If I remove the TextBlock like this :
<GroupBox>
<GroupBox.Template>
<ControlTemplate TargetType="{x:Type GroupBox}">
<ContentPresenter TextElement.FontSize="28" />
</ControlTemplate>
</GroupBox.Template>
Test
</GroupBox>
The text is now the displayed with 28 as FontSize.
Shouldn't the property value be inherited when I use a TextBlock ?
This other question How do I Change the FontFamily on a ContentPresenter? doesn't help, as it works only for default content too.
This question also : How do I Change the FontFamily on a ContentPresenter?.
Both works whe you use the default content handler, but fails when you manually create a textblock.
Edit: As demonstrated in this other question, I've tried by simply using a ContentControl :
<StackPanel>
<StackPanel.Resources>
<ControlTemplate x:Key="UsingBorderTemplate" TargetType="{x:Type ContentControl}">
<Border BorderBrush="Red" BorderThickness="1" TextElement.FontFamily="Courier New" Margin="5">
<ContentPresenter/>
</Border>
</ControlTemplate>
<ControlTemplate x:Key="MyTemplate" TargetType="{x:Type ContentControl}">
<ContentPresenter TextElement.FontFamily="Courier New" Margin="5" />
</ControlTemplate>
</StackPanel.Resources>
<ContentControl Template="{StaticResource MyTemplate}">
I'm courier new!
</ContentControl>
<ContentControl Template="{StaticResource MyTemplate}">
<TextBlock>I'm default!</TextBlock>
</ContentControl>
</StackPanel>
You can change the template from "MyTemplate" to "UsingBorderTemplate" with the same result.
I had an odd problem with ContentPresenter. I remember that I have analyzed the source of the problem and have found out that it was by design - Probably you have here the same issue.
Look at this post, maybe it helps you.
I think the text that the content presenter is presenting is the GroupBox.Header, and you may just be tacking another TextBox in there that isn't part of the Group Box.
In your first code block, add the line below and see if that works:
<GroupBox.Header>Test</GroupBox.Header>
HTH,
Berryl
How do I change the default textwrapping style of a button in WPF?
The obvious solution of:
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="TextWrapping" Value="Wrap"></Setter>
</Style>
doesn't work, because Textwrapping isn't a settable property here, apparently.
If I try:
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock Text="{Binding}" Foreground="White" FontSize="20" FontFamily="Global User Interface" TextWrapping="Wrap"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I just get a worthless response from the compiler:
Error 5 After a 'SetterBaseCollection' is in use (sealed), it cannot be modified.
Removing the ControlTemplate tag keeps the error.
The following attempt yields a different error:
<Setter Property="TextBlock">
<TextBlock Text="{Binding}" Foreground="White" FontSize="20" FontFamily="Global User Interface" TextWrapping="Wrap"/>
</Setter>
Error 5 The type 'Setter' does not support direct content.
I see that I can set the textwrapping for each button individually, but that's pretty asinine. How can I do it as a style? What are the magic words?
And for future reference, where can I find a list of these magic words, so I can just do this on my own? The MSDN entry is pretty useless when I try to find out about which properties can be set by the setter.
To expand Eric's answer with an example:-
<Button Name="btnName" Width="50" Height="40">
<TextBlock Text="Some long text" TextWrapping="Wrap" TextAlignment="Center"/>
</Button>
I solved this problem by adding a TextBlock to the button, and using it to display the button text instead of the button's Content property. Be sure to set the TextBlock's height property to Auto, so that it grows in height to accommodate the number of lines of text as it wraps.
Your second version should work, and does for me, with the caveat that you need to change the TextBlock Text binding:
<!-- in Window.Resources -->
<Style x:Key="fie" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock Text="{TemplateBinding Content}" FontSize="20" TextWrapping="Wrap"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- then -->
<Button Style="{StaticResource fie}">verylongcaptiongoeshereandwraps/Button>
Note this completely replaces the button style (i.e. you will need to create your own button chrome if you want it).
Regarding your second question, all writeable dependency properties can be set using a Setter. The reason you were unable to set TextWrapping on a Button via a style is that Button does not have a TextWrapping dependency property (or indeed any TextWrapping property). There are no "magic words," just the names of dependency properties.
<Style TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Here's an example of Eric's answer in C# code-behind:
var MyButton = new Button();
MyButton.Content = new TextBlock() {
FontSize = 25,
Text = "Hello world, I'm a pretty long button!",
TextAlignment = TextAlignment.Center,
TextWrapping = TextWrapping.Wrap
};
To expand #Rob's answer with #fadden's comment:
<Button Width="50" Height="40">
<AccessText Text="_Some long text" TextWrapping="Wrap" TextAlignment="Center"/>
</Button>
The TextBlock control does not support keyboard hotkeys (_).