【问题标题】:XAML WPF Checkbox doesn´t visualize when .ischecked property changed当 .ischecked 属性更改时,XAML WPF Checkbox 不可视化
【发布时间】:2013-01-24 08:22:54
【问题描述】:

我是 WPF 和 XAML 编程的初学者。而且我的英语不是很好。对不起!

这是我的问题。我想要一个自定义复选框。只有当鼠标悬停并且光标应更改为帮助光标时才应启用它。

效果很好!当我单击复选框时,复选框变为.ischeckd = true 并在复选框中显示小箭头。当我再次单击复选框时,.ischecked 属性更改为false,复选框中的小箭头消失了。到目前为止,一切都很好! 但是,当我以编程方式设置 .ischeckd 值时,它不会更改复选框中的小箭头。因此,即使 .ischecked 属性为假,程序的用户仍认为复选框是“活动的”。希望你能理解我的问题... 这是 XAML 代码:

</Style>
    <Storyboard x:Key="StoryboardCheckBox"/>
    <Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <Grid Margin="0,0,42,0">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsEnabled)" Storyboard.TargetName="checkBox">
                                            <DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <CheckBox x:Name="checkBox" Content="Gutschrift" FontSize="13.333" IsEnabled="False" Cursor="Help" Margin="0,0,4.937,0"/>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="" Margin="78.063,9.723,0,0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

... 一些标签和按钮以及其他东西

<CheckBox Content="CheckBox" HorizontalAlignment="Left" Height="23" Margin="29,0,0,44" Grid.Row="1" Style="{DynamicResource CheckBoxStyle1}" VerticalAlignment="Bottom" Width="125" Name="CheckBoxGutschrift" IsChecked="False" DataContext="{Binding}" Checked="CheckBoxGutschrift_Checked" Unchecked="CheckBoxGutschrift_Unchecked" ClickMode="Press" IsTabStop="False" />

希望你能理解我的问题!非常感谢您的回答,Ruediger

【问题讨论】:

    标签: wpf xaml checkbox


    【解决方案1】:

    您忘记在控件模板中绑定内部复选框的IsChecked 属性:

    <CheckBox x:Name="checkBox" IsChecked="{TemplateBinding IsChecked}" ...
    

    【讨论】:

    • 像魅力一样工作!非常非常感谢!
    猜你喜欢
    • 2016-04-26
    • 2016-01-13
    • 1970-01-01
    • 2011-01-25
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多