none
Expander 模板问题 RRS feed

  • 问题

  • <Style x:Key="expanderStyle" TargetType="{x:Type Expander}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Expander}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30" /> <RowDefinition x:Name="ContentRow" Height="0" /> </Grid.RowDefinitions> <Border x:Name="Border" Grid.Row="0"> <CheckBox Content="{TemplateBinding Header}" VerticalAlignment="Center" VerticalContentAlignment="Center" IsChecked="{TemplateBinding IsExpanded}"> </CheckBox> </Border> <Border x:Name="Content" Grid.Row="1"> <ContentPresenter Margin="4" /> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsExpanded" Value="True"> <Setter TargetName="ContentRow" Property="Height" Value="{Binding DesiredHeight, ElementName=Content}" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

    重写了Expander模板,我就是想实现勾选CheckBox的时候展开Expander,取消勾选的时候折叠。但是运行时达不到预期效果,不知道是哪里使用不正确。


    2015年2月10日 8:35

答案

  • 你好 blwuer,

    我们先看一下TemplateBinding吧:

    https://msdn.microsoft.com/en-us/library/ms742882(v=vs.110).aspx

    “A TemplateBinding is always a one-way binding, even if properties involved default to two-way binding. Both properties involved must be dependency properties.”

    TemplateBinding是One-way的,那么你看下这里:

     IsChecked="{TemplateBinding IsExpanded}"

    这里应该代表的跟你的意思恰恰相反,“勾选CheckBox的时候展开Expander”,这里的写法应该是展开和不展开只反应给CheckBox. 你这里是不是应该做一个Two-way的绑定而不是One-way的。



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 blwuer 2015年3月13日 6:03
    2015年2月11日 9:51
    版主