1

ファイル名を含むいくつかのテキストボックスに「参照」ボタンを添付しようとしています。

私はこのようなものを目指していました:

<TextBox Text="c:\Filename.txt" Template="{StaticResource FileBrowser}"/>

私が宣言したコントロールテンプレートは次のとおりです。

<Window.Resources>
    <ControlTemplate x:Key="FileBrowser" TargetType="{x:Type TextBox}">
        <Grid Grid.Row="{TemplateBinding Grid.Row}" Grid.Column="{TemplateBinding Grid.Column}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <ContentPresenter Grid.Column="0" Grid.Row="0"/>
            <Button Grid.Column="1" Content="..."/>
        </Grid>
    </ControlTemplate>
</Window.Resources>

しかし、これを使おうとすると、TextBoxはもうありません。空白スペースのみで、その後に「...」が続きます

誰かが私が間違っていることを知っていますか?これは、新しいUserControlを作成することによってのみ解決できる種類のものですか?

4

4 に答える 4

3

TextBoxは、ScrollViewerタイプの「PART_ContentHost」という名前のTemplatePartを定義します。コード内のすべてのテキスト処理は、テンプレートに存在するこの要素に依存しています。テンプレートに含めていないため、TextBoxは基本的に壊れています。代わりに、ContentPresenterを次のように変更します。

<ScrollViewer x:Name="PART_ContentHost" Grid.Column="0" Grid.Row="0"/>
于 2010-06-29T12:09:46.067 に答える
0

ContentPresenterは、コンテンツ(テキスト文字列)を提供するだけで、テキストボックスのレイアウトは提供しません。

このシナリオでは、UsercontrolまたはContentControlを作成します。

于 2010-06-29T07:07:18.780 に答える
0

TextBoxはContentControlではないため、ContentPresenterを理解することはできません。

ただし、TextプロパティにTemplateBindingを使用して、ContentPresenterを正しい方向に向けることができます。

<ContentPresenter Grid.Column="0" Grid.Row="0" Content="{TemplateBinding Text}" />

これで、テキストが新しいコンテンツとして表示されます。

また、ContentPresenterの使用を再検討し、ContentPresenterの代わりにTextBlockを使用することもできます。

<TextBlock Grid.Column="0" Grid.Row="0" Text="{TemplateBinding Text}" />
于 2010-06-29T07:25:23.773 に答える
0

このxamlを試してみてください...

    <Window.Resources>
    <LinearGradientBrush x:Key="NormalBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#EEE" Offset="0.0"/>
        <GradientStop Color="#CCC" Offset="1.0"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="NormalBorderBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#CCC" Offset="0.0"/>
        <GradientStop Color="#444" Offset="1.0"/>
    </LinearGradientBrush>

    <!-- LightBrush is used for content areas such as Menu, Tab Control background -->
    <LinearGradientBrush x:Key="LightBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#FFF" Offset="0.0"/>
        <GradientStop Color="#EEE" Offset="1.0"/>
    </LinearGradientBrush>

    <!-- MouseOverBrush is used for MouseOver in Button, Radio Button, CheckBox -->
    <LinearGradientBrush x:Key="MouseOverBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#FFF" Offset="0.0"/>
        <GradientStop Color="#AAA" Offset="1.0"/>
    </LinearGradientBrush>

    <!-- PressedBrush is used for Pressed in Button, Radio Button, CheckBox -->
    <LinearGradientBrush x:Key="PressedBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#BBB" Offset="0.0"/>
        <GradientStop Color="#EEE" Offset="0.1"/>
        <GradientStop Color="#EEE" Offset="0.9"/>
        <GradientStop Color="#FFF" Offset="1.0"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="PressedBorderBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#444" Offset="0.0"/>
        <GradientStop Color="#888" Offset="1.0"/>
    </LinearGradientBrush>

    <!-- SelectedBackgroundBrush is used for the Selected item in ListBoxItem, ComboBoxItem-->
    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD"/>

    <!-- Disabled Brushes are used for the Disabled look of each control -->
    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888"/>
    <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE"/>
    <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA"/>

    <!-- Used for background of ScrollViewer, TreeView, ListBox, Expander, TextBox, Tab Control -->
    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF"/>

    <!-- DefaultedBorderBrush is used to show KeyBoardFocus -->
    <LinearGradientBrush x:Key="DefaultedBorderBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#777" Offset="0.0"/>
        <GradientStop Color="#000" Offset="1.0"/>
    </LinearGradientBrush>

    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888"/>
    <SolidColorBrush x:Key="LightBorderBrush" Color="#AAA"/>
    <SolidColorBrush x:Key="LightColorBrush" Color="#DDD"/>

    <!-- Used for Checkmark, Radio button, TreeViewItem, Expander ToggleButton glyphs -->
    <SolidColorBrush x:Key="GlyphBrush" Color="#444"/>
    <!-- Simple TextBox -->
    <Style x:Key="SimpleTextBox" TargetType="{x:Type TextBox}">
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Border x:Name="Border" Background="{DynamicResource WindowBackgroundBrush}" BorderBrush="{DynamicResource SolidBorderBrush}" BorderThickness="1" Padding="2" CornerRadius="2">

                            <!-- The implementation places the Content into the ScrollViewer. It must be named PART_ContentHost for the control to function -->
                            <ScrollViewer Margin="0" x:Name="PART_ContentHost" Style="{DynamicResource SimpleScrollViewer}" Background="{TemplateBinding Background}"/>

                        </Border>
                        <Button HorizontalAlignment="Stretch" Width="25" Content="..." Grid.Column="1" Margin="1"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
                            <Setter Property="BorderBrush" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
                            <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
<Grid>
    <TextBox Style="{StaticResource SimpleTextBox}" Height="25"></TextBox>
</Grid>
于 2010-06-29T09:44:24.443 に答える