Setting an attached property by a Setter element?

H

Henrik Dahl

I have the fragment below as contents of a <Grid> element. The idea is that
if the checkbox is checked the <TreeView> element should only span one
instead of two columns. When I check the checkbox the background of the
treeview turns red, but the treeview continues to span 2 instead of 1
column. It's a bit like the attached property Grid.ColumnSpan does not get
assigned by the <Setter Property="Grid.ColumnSpan" Value="1"/> at all. What
should I do in order to achieve what I want?


Best regards,

Henrik Dahl

<TreeView

x:Name="Blas"

ItemsSource="{Binding Path=BlasRoot.Children}" d:LayoutOverrides="Width"
Margin="0,0,0,8" Grid.Column="0" Grid.ColumnSpan="2">

<TreeView.Style>

<Style>

<Style.Triggers>

<DataTrigger Binding="{Binding ElementName=chkPosition,
Path=IsChecked}" Value="True">

<Setter Property="Grid.ColumnSpan" Value="1"/>

<Setter Property="TreeView.Background" Value="Red"/>

</DataTrigger>

</Style.Triggers>

</Style>

</TreeView.Style>

</TreeView>

<CheckBox HorizontalAlignment="Left" Margin="-50,0,0,0" x:Name="chkPosition"
VerticalAlignment="Top" Content="CheckBox">

</CheckBox>
 
W

Walter Wang [MSFT]

Hi Henrik,

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Henrik Dahl

Hello Walter,

Thank you very much. I look forward for further information.


Best regards,

Henrik Dahl
 
W

Walter Wang [MSFT]

Hi Henrik,

The problem is that that property is also being set on the TreeView itself.
When a property is specified directly on an element, and on the element's
style, the conflict resolution gives precedence to the value on the
element. I.e., a local value wins over a style value.

You could find more information here:

#Dependency Property Value Precedence
http://msdn2.microsoft.com/en-us/library/ms743230.aspx


To fix it, we need to remove the Grid.ColumnSpan attribute in the TreeView
element and set it using a Setter:

<TreeView
x:Name="Blas"
ItemsSource="{Binding Path=BlasRoot.Children}"
d:LayoutOverrides="Width"
Margin="0,0,0,8" Grid.Column="0">
<TreeView.Style>
<Style>
<Setter Property="Grid.ColumnSpan" Value="2" />


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Henrik Dahl

Hello Walter,

Thank you very much for your assitance, as always! I must say that I did not
know that the attribute on the TreeView element also takes precedence over
the style when the style is applied after construction. I thought it just
took precedence over during the construction phase.


Best regards,

Henrik Dahl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top