0

TextBox に固定テキストを動的に追加するにはどうすればよいですか? 「固定テキスト」とは、ユーザーの入力によって削除できないテキストを意味します。

たとえば、CMD のパス:

C:\Program Files>cd ..
C:\>
4

4 に答える 4

2

編集可能なテキストボックスが必要だと想定しています。このテキストボックスの先頭には、ユーザーが編集できない固定テキストが含まれています。もしそうなら、これはうまくいくようです-それはBlendで抽出された標準のテキストボックススタイルに基づいています...

xamlルートに次の名前空間宣言が必要です。

     xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"

次に、このテンプレートを使用します。

    <ControlTemplate TargetType="{x:Type TextBox}">
        <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true">
        <StackPanel Orientation="Horizontal">
            <TextBlock VerticalAlignment="Center">This is fixed:</TextBlock>
            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
          </StackPanel>
    </Microsoft_Windows_Themes:ListBoxChrome>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

これをカスタムコントロールまたはユーザーコントロールでラップする場合は、カスタムプロパティを使用してプログラムで固定テキストを設定できます。

于 2012-04-27T15:57:25.903 に答える
0

テキストを動的に入力する場合、TextBox IsReadOnly = true? MVVM を使用して IsReadOnly を ViewModel のプロパティにバインドする場合、その ViewModel がテキストを入力するとき

于 2012-04-27T15:50:48.717 に答える
0

代わりにテキストブロックを使用できます。テキストボックスを使用する必要がある場合は、IsReadOnly プロパティを "True" に変更して読み取り専用にすることができます。

于 2012-04-27T15:51:10.633 に答える
-2
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (textBox1.SelectionStart < LengthOfFixedString)
        e.SuppressKeyPress = true;
}
于 2012-04-27T15:57:48.023 に答える