Reputation: 101
I'm trying to extend TextBox Default Style https://msdn.microsoft.com/en-us/library/windows/apps/mt299154.aspx
I whould like to have text box corners rounded.
<Border
x:Name="BackgroundElement"
Grid.Row="1"
Background="{TemplateBinding Background}"
Margin="{TemplateBinding BorderThickness}"
CornerRadius="{Binding Source={RelativeSource TemplatedParent}, Path=(icp:IcpExtend.CornerRadius)}"
Opacity="{ThemeResource TextControlBackgroundRestOpacity}"
Grid.ColumnSpan="2"
Grid.Column="0" />
<Border
x:Name="BorderElement"
Grid.Column="0"
Grid.Row="1"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{Binding Source={RelativeSource TemplatedParent}, Path=(icp:IcpExtend.CornerRadius)}"
Grid.ColumnSpan="2" />
Here is attached property class
[Bindable]
public class IcpExtend
{
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(
"CornerRadius", typeof(CornerRadius), typeof(IcpExtend), new PropertyMetadata(default(CornerRadius)));
public static void SetCornerRadius(DependencyObject element, CornerRadius value)
{
element.SetValue(CornerRadiusProperty, value);
}
public static CornerRadius GetCornerRadius(DependencyObject element)
{
return (CornerRadius) element.GetValue(CornerRadiusProperty);
}
}
This is TextBox element on the page
<TextBox
icp:IcpExtend.CornerRadius="10"
Grid.Row="0"
Grid.Column="1"
PlaceholderText="Email"
InputScope="EmailSmtpAddress"
IsSpellCheckEnabled="False"
Text="{Binding Path=Email, Mode=TwoWay}" />
Solution is not working (corners are not rounded)... How to bind to attached property inside Default Style -> ControlTemplate ?!
Upvotes: 0
Views: 737
Reputation: 34286
That should work. Does it work if you hardcode a CornerRadius in the template? If not, then there's something wrong with your style. You should put your TextBox style into App.xaml resources and the style should only have TargetType="TextBox"
and no key.
Also you should be able to use a TemplateBinding instead of RelativeSource in the template:
CornerRadius="{TemplateBinding icp:IcpExtend.CornerRadius}"
Another thing to keep in mind is that this style is for Windows SDK 10586 (as mentioned on that page). I'm using SDK 14393, and the default style for the TextBox control is different from that in SDK 10586. Make sure you're using the correct style. You can find the styles in C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.?????.0\Generic\generic.xaml
.
Upvotes: 1