【问题标题】:How to make value of a custom control editable upon receiving focus如何在接收焦点时使自定义控件的值可编辑
【发布时间】:2013-07-01 20:59:56
【问题描述】:

我创建了一个自定义 WPF 控件,它显示 Weight 属性的值,它是一个 int。当用户单击自定义控件或控件获得焦点时,用户应该能够编辑 Weight 属性的值。

为此,我尝试了以下方法:

  • 如果控件处于“非活动状态”,则会显示 Weight 属性 在文本块中。
  • 如果控件获得焦点,Weight 属性 将显示在 TextBox 中。

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary1">
    
    <Style TargetType="{x:Type local:CustomControl1}">
        <Style.Resources>
            <ControlTemplate x:Key="Control_Focused" >
                <Border Background="Green"
                            BorderBrush="Black"
                             CornerRadius="50" Width="40" Height="40">
                    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                       <TextBox Width="35"
                                    Text="{Binding Mode=TwoWay,Path=Model.Weight,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:CustomControl1}}
                                    ,Converter={x:Static local:CustomControl1.IntToStringConverter}}" />
    
                    </StackPanel>
                </Border>
            </ControlTemplate>
    
            <ControlTemplate x:Key="Control">
                <Border Background="Green"
                            BorderBrush="Black"
                           CornerRadius="50" Width="40" Height="40">
                    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                        <TextBlock x:Name="PART_TextBlockWeighted" Text="{Binding Mode=TwoWay,Path=Model.Weight,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:CustomControl1}}}" 
                                   HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </StackPanel>
                </Border>
            </ControlTemplate>
    
    
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Template" Value="{StaticResource Control_Focused}" ></Setter>
            </Trigger>
            <Trigger Property="IsFocused" Value="False">
                <Setter Property="Template" Value="{StaticResource Control}" ></Setter>
            </Trigger>
        </Style.Triggers>
    
    </Style>
    

这种自定义控件样式的问题在于,当用户点击TextBox时,触发器会将控件模板更改为TextBlock。我该如何解决这个问题?

【问题讨论】:

  • 为什么不只使用一个文本框并将IsReadOnly="True" 设置为不可编辑,如果需要也为其提供不同的样式。

标签: c# .net wpf xaml


【解决方案1】:

尝试改用IsKeyboardFocusWithin 属性。我相信这会满足您的需求。

<Style.Triggers>
    <Trigger Property="IsKeyboardFocusWithin" Value="True">
        <Setter Property="Template" Value="{StaticResource Control_Focused}" ></Setter>
    </Trigger>
    <Trigger Property="IsKeyboardFocusWithin" Value="False">
        <Setter Property="Template" Value="{StaticResource Control}" ></Setter>
    </Trigger>
</Style.Triggers>

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多