【问题标题】:Change Label content with IsMouseOver on checkbox在复选框上使用 IsMouseOver 更改标签内容
【发布时间】:2013-01-03 13:32:48
【问题描述】:

嘿,

我正在创建一个按钮,上面有一个复选框。现在,当用户将鼠标悬停在复选框上时,我想更改按钮上的文本。

这是我目前拥有的:

    <Button Grid.Row="1" Margin="0,0,136,12" Name="btnRefresh" Height="28" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="120" Click="btnRefresh_Click">
        <Grid Width="111">
            <CheckBox  Height="16" HorizontalAlignment="Left" Margin="0,2,0,0" Name="cbAutoRefresh" VerticalContentAlignment="Center">
                <CheckBox.Style>
                    <Style TargetType="{x:Type CheckBox}">
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Content" Value="Auto Refresh" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </CheckBox.Style>
            </CheckBox>
            <Label Name="btnLabel" Margin="32,-4,25,-4">Refresh</Label>
        </Grid>
    </Button>

但是像这样它改变了复选框的内容,我怎样才能改变btnLabel的内容?

【问题讨论】:

    标签: c# wpf button triggers


    【解决方案1】:

    目前您只是在修改 ComboBox 的样式,这就是为什么 Trigger 只是更改 ComboBox 的内容。

    要监听触发器中的其他控件属性,您可以像这样使用数据绑定:

    <Label Name="btnLabel" Margin="32,-4,25,-4">
        <Label.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=comboBoxName, Path=IsMouseOver}" Value="False"
                         <Setter Property="Content" Value="Refresh"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=comboBoxName, Path=IsMouseOver}" Value="True"
                         <Setter Property="Content" Value="Auto Refresh"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    
    </Label>
    

    【讨论】:

    • 这给了我错误:Cannot resolve the Style Property 'Text'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.
    猜你喜欢
    • 2020-04-25
    • 2013-06-07
    • 2012-06-05
    • 1970-01-01
    • 2018-06-14
    • 2017-03-21
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    相关资源
    最近更新 更多