1

コントロールテンプレートを使用してテキストボックスのスタイルを変更しました。スタイリングはうまく機能しますが、テキストボックスで変更された後、プログラムでテキストボックスからテキスト値を取得できなくなりました。

これが私がスタイルを使う方法です:

     <TextBox
        x:Name="KernTextBox"
        Style="{StaticResource MediumTextBoxStyle}"
        TextWrapping="Wrap"
        MinLines="1"
        MaxLines="5"
        VerticalScrollBarVisibility="Auto"
        TextChanged="TextChanged"
        />

これが機能しないものです。

  get { return KernTextBox.Text; }

スタイルは次のとおりです。

<Style x:Key="MediumTextBoxStyle" TargetType="TextBox">
    <Setter Property="FontSize" Value="18" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="ReadOnly">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonRectangle"
                                        Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource BackgroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonRectangle"
                                        Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource BackgroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>

                        </VisualStateGroup>

                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked" />
                            <VisualState x:Name="Unchecked"/>
                        </VisualStateGroup>

                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused" />
                            <VisualState x:Name="Unfocused"/>
                        </VisualStateGroup>

                    </VisualStateManager.VisualStateGroups>

                    <Rectangle 
                        x:Name="ButtonRectangle" 
                        Stroke="Transparent" 
                        UseLayoutRounding="False" 
                        Fill="{StaticResource BackgroundBrush}">
                    </Rectangle>

                    <TextBox 
                        x:Name="ButtonText" 
                        Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                        FontSize="{TemplateBinding FontSize}"
                        HorizontalAlignment="Stretch" 
                        VerticalAlignment="Center" 
                        Margin="2 0"
                        TextWrapping="{TemplateBinding TextWrapping}"
                        MinLines="{TemplateBinding MinLines}"
                        MaxLines="{TemplateBinding MaxLines}"
                        VerticalScrollBarVisibility="{TemplateBinding VerticalScrollBarVisibility}"
                        Padding="{TemplateBinding Padding}"
                        Foreground="{StaticResource ForegroundBrush}"
                        Background="{StaticResource BackgroundBrush}"
                        >
                    </TextBox>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

編集:

最終的な作業スタイル:

<Style 
    x:Key="TextBoxStyle" 
    TargetType="{x:Type TextBox}">
    <Setter
        Property="Foreground"
        Value="{StaticResource ForegroundBrush}" />
    <Setter
        Property="FontSize"
        Value="18" />
    <Setter 
        Property="CaretBrush"
        Value="{StaticResource ForegroundBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border 
                    Name="Border"
                    Background="{StaticResource BackgroundBrush}"
                    BorderBrush="{StaticResource ForegroundBrush}"
                    BorderThickness="1" >
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource ForegroundBrush}"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BackgroundBrush}"/>
                        <Setter Property="Foreground" Value="{StaticResource BackgroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsReadOnly" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource ForegroundBrush}"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BackgroundBrush}"/>
                        <Setter Property="Foreground" Value="{StaticResource BackgroundBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

2 に答える 2

1

テキストボックスをテキストボックス内に配置する理由はありますか?テキストボックスの元のスタイルとテンプレートをチェックアウトし、そこから変更します。私の推測では、テキストボックスは内部でTextBoxView(Scrollviewerに非表示)を使用し、TextプロパティはTemplateBindingを介してバインドされませんが、これらの内部クラス間で計算されます。したがって、おそらくそれが、テキストプロパティをコントロールテンプレートのテキストボックステキストにバインドできない理由です。

于 2012-06-15T01:07:11.403 に答える
1

スタイルで、内部TextプロパティをTemplate Textプロパティにバインドするときに、更新ソーストリガーを追加すると、PropertyChangedフォーカスを失っていなくてもテキストが更新されます。

Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

テキストボックスのスタイルを設定する方法がうまく機能しなかった理由は、デフォルトでは、テキストプロパティは、フォーカスがコントロールから削除されるまで、バインドされたプロパティを更新しないためです。TextChangedイベント内でTextプロパティを使用しようとしていると思いますが、その時点ではTextBoxからフォーカスが削除されていません。

テキストバインディングに提案した追加により、テキストが変更されるたびにプロパティが強制的に更新され、TextChangedイベントが発生する直前に更新されます。

于 2012-06-15T09:19:13.710 に答える