按钮2文本转换

时间:2015-02-17 10:23:09

标签: wpf silverlight

wpf按钮上可以有2个文本吗?

按钮1 enter image description here

 <Button>
   <StackPanel Orientation="Vertical">
       <TextBlock FontSize="8" HorizontalAlignment="Right">Secondary</TextBlock>
        <TextBlock>Primary</TextBlock>
   </StackPanel>
</Button>

我有另一个按钮

将Button2

<Button>Toggle Text</Button>

点击此按钮时,我想更改(切换)第一个按钮上的文字。 这可能是WPF吗?

 private void Btn_Click(object sender, RoutedEventArgs e)
        {
            var temp = Primary.Text;

            Primary.Text = Secondary.Text;
            Secondary.Text = temp;

        }

enter image description here

2 个答案:

答案 0 :(得分:2)

你可以一起使用风格和触发器

<Button HorizontalAlignment="Left" Height="98" Margin="119,83,0,0" VerticalAlignment="Top" Width="327" x:Name="B">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid Width="300" Height="50">
                                <TextBlock Text="secondary" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                                <TextBlock Text="Primary" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontSize="22" />
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Switch,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="true">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <Grid Width="300" Height="50">
                                        <TextBlock Text="primary" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                                        <TextBlock Text="secondary" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontSize="22" />
                                    </Grid>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

        </Button.Style>
    </Button>

<Button Content="TestBuuton" HorizontalAlignment="Left" Height="66" Margin="336,330,0,0" VerticalAlignment="Top" Width="222" Command="{Binding TestCommand}" />

在您的viewview模型中,您可以像这样切换值

    RelayCommand test;
    public RelayCommand TestCommand
    {
        get
        {
            if (test == null)
            {
                test = new RelayCommand(p => execute(p));
            }
            return test;
        }
    }


void execute(object parameter)
    {
        if (Switch)
        {
            Switch = false;
        }
        else
        {
            Switch = true;
        }
        RaisePropertyChanged("Switch");
    }

enter image description here  点击后 enter image description here

我希望这是你在寻找

答案 1 :(得分:2)

XAML代码:

<Grid>
    <Button x:Name="MainButton" Height="70" Width="213" Click="MainButton_Click" >
        <Grid Height="{Binding ElementName=MainButton, Path=ActualHeight}" Width="{Binding ElementName=MainButton, Path=ActualWidth}">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="2*"/>                    
            </Grid.RowDefinitions>                
            <TextBlock x:Name="TextOne" Grid.Row="0" HorizontalAlignment="Right" Padding="10 0">Primary</TextBlock>
            <TextBlock x:Name="TextTwo" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">Secondary</TextBlock>
        </Grid>            
    </Button>
</Grid>

C#代码隐藏:

 public MainWindow()
    {
        InitializeComponent();
    }

    private void MainButton_Click(object sender, RoutedEventArgs e)
    {
        string aux = TextOne.Text;

        TextOne.Text = TextTwo.Text;
        TextTwo.Text = aux;
    }

在上面的例子中,我使用了一个丑陋的按钮,但它取决于你,使它更美丽。它也不遵循MVVM模式。当您准备将其重构为MVVM时,只需使用Command而不管Click事件,取下“X:Names”并从代码中的变量中获取按钮文本。

希望有所帮助。