2015-10-21 6 views
0
<Button Name="Dog" Content="Dog" 
Height="80" FontSize="17" 
Width="{Binding RelativeSource={RelativeSource Self},Path=Height}"  
Click="Dog_Click"/> 

Мой вопрос: как это сделать ??? Обычно содержимое кнопки было скрыто, когда я нажимаю кнопку, содержимое «Собака» становится видимым. БлагодаряОтображение содержимого кнопки при нажатии кнопки

+1

вы пытались что-нибудь еще? у вас есть идеи, как это сделать? – Muds

ответ

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> 

Используйте RelativeSource привязки в случае, если вы хотите, чтобы связать некоторые DataTemplate внутреннее свойство conntrol в контексте данных родителя (кнопка). Например: {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=DataContext.DataContextData}.

Просмотреть модель, связанная с видимостью изображения на основе MVVM, управляется на уровне viewmodel.
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(); 
     } 
    }} 

С уважением,

0

Вы можете установить контроль ребенка на кнопку, как:

<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 Event просто установить видимость Visibility.Visible. Не забудьте установить значение по умолчанию для Visibility.Hidden

Смежные вопросы