Pants WPF Error

http://blogs.charteris.com/blogs/patl/archive/2007/03.aspx

Thanks Patrick for the post.

<Style x:Key="DisplayOnly" TargetType="{x:Type TextBox}"   BasedOn="{StaticResource {x:Type Control}}">
  <Setter Property="Control.BorderThickness" Value="0,0,0,0" />
  <Setter Property="TextBox.IsReadOnly" Value="True" />
  <Setter Property="Control.Background" Value="Transparent"/>
</Style>
 
<Style x:Key="DateDueStyle" BasedOn="{StaticResource DisplayOnly}">
  <Setter Property="Control.Foreground" Value="Red"/>
  <Setter Property="Control.FontWeight" Value="Bold"/>
</Style>

Trying to base a style on another style, was giving me the rather cryptic error;
{“Cannot convert the value in attribute ‘Style’ to object of type ‘System.Windows.Style’. Can only base on a Style with target type that is base type ‘IFrameworkInputElement’. Error at object ‘System.Windows.Controls.TextBox’ in markup file ‘xxx;xamlfile.xaml’ Line 111 Position 47.”}

I found Patrick’s post (Linked top of this post) and initially was going to move the DisplayOnly style stuff into the local DateDueStyle style. But that wasn’t right, I wanted the DisplayOnly to remain where it was (It’s in a different assembly and gets loaded into the ResourceDictionary).

Adding a TargetType fixed the problem. So it looks like the TargetStyle does not come down to your style when you use BasedOn. The real problem was that it wasnt set.

.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: #a31515; }.cb3 { color: red; }

<Style x:Key="DateDueStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource DisplayOnly}">
  <Setter Property="Control.Foreground" Value="Red"/>
  <Setter Property="Control.FontWeight" Value="Bold"/>
</Style>
Advertisements

3 Replies to “Pants WPF Error”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: