按下Enter键上的WPF Set元素属性

时间:2014-09-25 09:52:56

标签: wpf mvvm properties triggers keypress

我为我的Label定义了自定义样式,如下面的代码:

<Style x:Key="GridLabelStyle" TargetType="{x:Type Label}">
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Height" Value="30"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="70*"/>
                        <ColumnDefinition Width="150"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <Label Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag, Mode=OneWay}"/>

                    <Border Grid.Column="1" Name="grayborder" BorderBrush="#B2E8E4E4" BorderThickness="1" CornerRadius="2" Background="White">
                        <Grid>
                            <TextBlock Name="textblock" VerticalAlignment="Center" Margin="3" 
                                        Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Mode=TwoWay}" />
                            <TextBox Name="textbox" VerticalContentAlignment="Center" BorderThickness="0"
                                        Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

                            </TextBox>

                        </Grid>
                    </Border>

                    <ToggleButton Grid.Column="2" Name="Edit" Margin="5,0,0,0" ContentTemplate="{StaticResource editicon}" Style="{DynamicResource NoStyleToggleButton}"/>
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger SourceName="Edit" Property="IsChecked" Value="False">
                        <Setter TargetName="textblock" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="textbox" Property="Visibility" Value="Collapsed"/>

                        <!--<Setter TargetName="Edit" Property="Command" Value="{Binding UpdateElementCommand}"/>-->
                    </Trigger>
                    <Trigger SourceName="Edit" Property="IsChecked" Value="True">
                        <Setter TargetName="textblock" Property="Visibility" Value="Collapsed"/>
                        <Setter TargetName="textbox" Property="Visibility" Value="Visible"/>

                        <Setter TargetName="Edit" Property="ContentTemplate" Value="{StaticResource saveicon}"/>
                        <Setter TargetName="grayborder" Property="BorderBrush" Value="Gray"/>
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我想要做的主要是处理元素名称“textbox”上按键(Enter)的触发器,并将元素名称“edit”上的属性设置为IsChecked = False。

我正在使用.NET 3.5和MVVM。

是否可以在没有任何C#代码的情况下执行此操作?

此致

0 个答案:

没有答案