1

我正在尝试使用具有编辑状态的项目构建一个 ListBox。我为 Normal 和 Edit 模式创建了一个单独的 DataTemplate,但是一旦我更改了 ContentPresenter 上的 ContentTemplate,我就会失去来自按钮与 DataTempaltes 的事件冒泡(这意味着单击按钮不再有任何动作)。

这是代码:

<UserControl.Resources>

    <DataTemplate x:Key="NormalDT">
        <StackPanel>
            <TextBlock Text="{Binding Property1}"/>
            <CheckBox IsChecked="{Binding Property2, Mode=TwoWay}"/>
            <Button Content="Test" Click="Button_Click" />
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="EditDT">
        <StackPanel>
            <TextBox Text="{Binding Property1}"/>
            <CheckBox IsChecked="{Binding Property2, Mode=TwoWay}"/>
            <Button Content="Test" Click="Button_Click" />
        </StackPanel>
    </DataTemplate>
    <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
        <Setter Property="Padding" Value="3"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor2"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="contentPresenterEdit">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="contentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="fillColor" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
                        <Rectangle x:Name="fillColor2" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
                        <ContentPresenter x:Name="contentPresenter" 
                                          ContentTemplate="{StaticResource NormalDT}" 
                                          Content="{TemplateBinding Content}" 
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                          Margin="{TemplateBinding Padding}"/>

                        <ContentPresenter x:Name="contentPresenterEdit" 
                                          ContentTemplate="{StaticResource EditDT}" 
                                          Content="{TemplateBinding Content}" 
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                          Margin="{TemplateBinding Padding}" Visibility="Collapsed"/>
                        <Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
    <ListBox Height="263" Margin="33,54,366,0" VerticalAlignment="Top" ItemTemplate="{StaticResource NormalDT}" ItemsSource="{Binding Collection}" ItemContainerStyle="{StaticResource ListBoxItemStyle1}"/>

</Grid>

关于如何让 button_click 事件在我的情况下工作的任何想法?

任何帮助是极大的赞赏!

谢谢。

4

1 回答 1

1

出于某种我不知道的原因,Visibility在您的焦点视觉状态中定义的动画导致了这个问题。

尝试制作动画Opacity,它应该可以工作。

更新

实际上,您的代码确实可以在 WPF 中使用。我认为这是 Silverlight 中的一个错误。如果您将MouseLeftButtonDown事件附加到 aTextBlock它实际上会触发,但是对于Buttonsand情况并非如此CheckBoxes

然后我怀疑可能是这样,但我不是 100% 确定...

在 Silverlight 5 之前,当 Visibility 设置为 Collapsed 时,ButtonBase 不会更改视觉状态,因此当控件再次可见时,它会保持在变为不可见之前的视觉状态。

于 2011-12-13T02:56:18.090 回答