单击按钮时显示按钮的内容

时间:2015-10-21 09:01:03

标签: c# wpf xaml

<p>user / <a class="active" href="link">james</a></p>

我的问题是:该怎么办? 通常隐藏按钮内容,当我点击按钮时,内容“狗”变得可见。 感谢

2 个答案:

答案 0 :(得分:1)

这是纯xaml解决方案,此处不涉及任何代码,按下按钮时图片将可见。

<Window x:Class="TriggeredButtonVisibility.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:triggeredButtonVisibility="clr-namespace:TriggeredButtonVisibility"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="ButtonStyleKey" TargetType="Button">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        <TextBlock x:Name="ButtonContentTextBlock" Text="Press Me" 
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"></TextBlock>
                        <Image x:Name="ButtonContentPicture" 
                               Source="Resources/Pic.jpg" 
                               Stretch="Fill" Visibility="Collapsed" 
                               Margin="50" IsHitTestVisible="False"/>
                    </Grid>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsPressed}"  Value="True">
                            <Setter TargetName="ButtonContentTextBlock" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="ButtonContentPicture" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource ButtonStyleKey}">Press Me</Button>
</Grid></Window>

如果要将某些DataTemplate内部conntrol属性绑定到父(按钮)数据上下文,请使用RelativeSource绑定。例如:{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=DataContext.DataContextData}

视图模型涉及MVVM方法图像可见性在视图模型级别上进行管理  1. Xaml:

<Window x:Class="TriggeredButtonVisibility.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:triggeredButtonVisibility="clr-namespace:TriggeredButtonVisibility"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <triggeredButtonVisibility:ButtonLogicViewmodel></triggeredButtonVisibility:ButtonLogicViewmodel>
</Window.DataContext>
<Window.Resources>
    <Style x:Key="ButtonStyleKey" TargetType="Button">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate DataType="{x:Type triggeredButtonVisibility:ButtonLogicViewmodel}">
                    <Grid>
                        <TextBlock x:Name="ButtonContentTextBlock" Text="Press Me" 
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"></TextBlock>
                        <Image x:Name="ButtonContentPicture" 
                               Source="Resources/Pic.jpg" 
                               Stretch="Fill" Visibility="Collapsed" 
                               Margin="50" IsHitTestVisible="False"/>
                    </Grid>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding  WasPressedAtOnse, UpdateSourceTrigger=PropertyChanged}"  Value="True">
                            <Setter TargetName="ButtonContentTextBlock" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="ButtonContentPicture" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource ButtonStyleKey}" Command="{Binding PressCommand}" Content="{Binding }"/></Grid></Window>

2。 ViewModel代码:

    public class ButtonLogicViewmodel:BaseObservableObject
{
    private bool _wasPressedAtOnse;
    private ICommand _pressCommand;

    public ButtonLogicViewmodel()
    {
        WasPressedAtOnse = false;
    }

    public ICommand PressCommand
    {
        get { return _pressCommand ?? (_pressCommand = new RelayCommand(OnPress)); }
    }

    private void OnPress()
    {
        WasPressedAtOnse = !WasPressedAtOnse;
    }

    public bool WasPressedAtOnse
    {
        get { return _wasPressedAtOnse; }
        set
        {
            _wasPressedAtOnse = value;
            OnPropertyChanged();
        }
    }}

的问候,

答案 1 :(得分:0)

您可以将子控件设置为Button,如:

<Button Name="Dog">
   <Button.Content>
    <!--Add a Grid here or whatever and bind Visibility={Binding Path=..., Mode="TwoWay", UpdateSourceTrigger="PropertyChanged"}-->
   </Button.Content>
</Button>

在Dog_Click事件中,只需将Visibility设置为Visibility.Visible即可。 别忘了将默认值设置为Visibility.Hidden