WPF의 내부 버튼이있는 텍스트 상자를 만드는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1404057

  •  05-07-2019
  •  | 
  •  

문제

나는 만들고 싶다 TextBox a Button 내부, A와 같은 것 DatePicker, 그러나 정확히는 아닙니다. 아니면 그럴 수도 있습니다 ComboBox 내부 TextBox, 따라서 모드를 전환 할 수 있습니다 TextBox.

도와주세요?

도움이 되었습니까?

해결책

Combobox 또는 Date Time Picker와 같은 것을 원한다면 새 컨트롤을 만들어야합니다.이 새 컨트롤 내부에는 텍스트 상자와 텍스트 상자의 프레임처럼 보이는 프레임 내부에 텍스트 상자와 버튼을 나란히 그 다음 TextBox를 다시정하십시오. 프레임이 없습니다.

"문서"내부에 버튼을 넣으려면 풍부한 편집 내에 버튼을 넣는 것이 좋습니다.

Combobox Control 템플릿을 참조하십시오 MSDN

다른 팁

이 링크가 도움이 될 수 있습니다. http://msdn.microsoft.com/en-us/library/ms752068(vs.85).aspx.

"TextBox의 ControlTemplate에는 컨텐츠 호스트 요소로 태그가 지정된 정확히 하나의 요소가 포함되어야합니다.이 요소는 텍스트 상자의 내용을 렌더링하는 데 사용됩니다. 컨텐츠 호스트로 요소를 태그하려면 특수 이름 Part_Contenthost를 할당합니다. 콘텐츠 호스트 요소는 스크롤 뷰어 또는 adornerDecorator 여야합니다. 콘텐츠 호스트 요소는 자식 요소를 호스팅하지 않을 수 있습니다. "

TextBox 컨트롤을 생성하고 이것을 추가하여 작동하는 것처럼 보이지만 이상적인 상황은 다른 텍스트 상자를 재현합니다.

<TextBox.Template>
 <ControlTemplate>
        <Grid>
            <Grid.ColumnDefinitions></Grid.ColumnDefinitions>
            <TextBox Grid.Column="0"></TextBox>
            <Button HorizontalAlignment="Right" Width="25" Grid.Column="1">
            </Button>
        </Grid>         
    </ControlTemplate>
</TextBox.Template>

TextBox 대신 RichTextBox를 사용할 수 있으며 FlowDocument를 지원하여 버튼을 배치 할 수 있습니다.

라벨을 사용하고 템플릿을 변경하여 버튼을 포함시킬 수 있습니다. 레이블과 텍스트 블록의 차이점에 대한 좋은 개요를 보려면 이 게시물.

이를 수행하는 올바른 방법은 TextBox에서 컨트롤 템플릿을 사용하는 것입니다. 아래와 같은 것. 텍스트 상자에서 물려 받고 버튼 박스라고 불리는 클래스 내부에서 이것을 사용했습니다. 그런 다음 DateBox, DateTimebox, SQLServerConnectbox 등과 같은 다른 사람들을 상속합니다.

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

<TextBox.Template>
    <ControlTemplate TargetType="{x:Type TextBoxBase}">
        <mwt:ListBoxChrome 
            Background="{TemplateBinding Panel.Background}"
            BorderBrush="{TemplateBinding Border.BorderBrush}"
            BorderThickness="{TemplateBinding Border.BorderThickness}"
            RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}"
            RenderFocused="{TemplateBinding UIElement.IsKeyboardFocusWithin}"
            Name="Bd"
            SnapsToDevicePixels="True">

            <DockPanel>
                <Button DockPanel.Dock="Right" Name="myButton" Padding="3,0" Click="myButton_Click">...</Button>
                <ScrollViewer Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"></ScrollViewer>
            </DockPanel>
        </mwt:ListBoxChrome>
        <ControlTemplate.Triggers>
            <Trigger Property="UIElement.IsEnabled">
                <Setter Property="Panel.Background" TargetName="Bd">
                    <Setter.Value>
                        <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
                    </Setter.Value>
                </Setter>
                <Setter Property="TextElement.Foreground">
                    <Setter.Value>
                        <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                    </Setter.Value>
                </Setter>
                <Trigger.Value>
                    <s:Boolean>False</s:Boolean>
                </Trigger.Value>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</TextBox.Template>

편집 : 텍스트 상자가 아닌 제어에서 상속되도록 사용중인 방법을 변경합니다. 컨트롤은 테두리, 텍스트 상자 및 버튼으로 구성되어 있기 때문에 잘 작동합니다. 위의 솔루션에 중점을두고있었습니다. 이것은 새로운 템플릿입니다. 제 컨트롤을 버튼 박스라고 불렀습니다.

<Style TargetType="{x:Type local:ButtonBox}">
    <Setter Property="Border.BorderThickness" Value="1"></Setter>
    <Setter Property="Border.BorderBrush">
        <Setter.Value>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,20" MappingMode="Absolute">
                <LinearGradientBrush.GradientStops>
                    <GradientStop Color="#FFABADB3" Offset="0.05" />
                    <GradientStop Color="#FFE2E3EA" Offset="0.07" />
                    <GradientStop Color="#FFE3E9EF" Offset="1" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ButtonBox}">
                <mwt:ListBoxChrome 
                    Background="{TemplateBinding Panel.Background}"
                    BorderThickness="{TemplateBinding Border.BorderThickness}"
                    BorderBrush="{TemplateBinding Border.BorderBrush}"
                    RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}"
                    RenderFocused="{TemplateBinding UIElement.IsKeyboardFocusWithin}"
                    Name="Bd"
                    SnapsToDevicePixels="True">
                    <DockPanel>
                        <Button
                            DockPanel.Dock="Right"
                            Name="PART_Button"
                            Height="0"
                            Style="{x:Null}"
                            Margin="0"
                            Padding="3,0"
                            Content="{TemplateBinding local:ButtonBox.ButtonContent}"
                            IsTabStop="False">                                
                        </Button>
                        <TextBox
                            BorderBrush="{x:Null}"
                            BorderThickness="0"
                            Margin="0"
                            Name="PART_ContentHost"
                            IsReadOnly="{TemplateBinding TextBox.IsReadOnly}"
                            Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=Text}">                                
                        </TextBox>
                        <!-- ScrollViewer Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" Margin="1"></ScrollViewer -->
                    </DockPanel>
                </mwt:ListBoxChrome>
                <ControlTemplate.Triggers>
                    <Trigger Property="UIElement.IsEnabled">
                        <Setter Property="Panel.Background" TargetName="Bd">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="TextElement.Foreground">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="IsTabStop" Value="False"></Setter>
</Style>

아래 코드와 동일하게 grid.column 만 사용하십시오

<TextBox x:Name="txtUrl" Grid.Column="1" Margin="2,2,0,2" VerticalAlignment="Center" Padding="2" PreviewKeyDown="txtUrl_PreviewKeyDown" GotFocus="txtUrl_GotFocus" PreviewMouseDown="txtUrl_PreviewMouseDown"> 

</TextBox>
<eo:BareButton x:Name="btnAddFavorite" Grid.Column=" 1"   HorizontalAlignment="Right" Style="{StaticResource WindowButtonStyle }" Margin="2" >
    <eo:BareButton.Template>
        <ControlTemplate TargetType="{x:Type eo:BareButton}">
            <Border x:Name="PART_Border" Width="22" Height="22" Background="Transparent" VerticalAlignment="Center" Margin="2,0,0,0" CornerRadius="2">
                <Path 
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
                                                            Fill="Yellow" 

            Data="M 2,9 L 8,8 10,2 13,8 19,9 15,13 16,19 10,15 5,19 6,13 2,9"
        SnapsToDevicePixels="false"
            Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type eo:BareButton}, Mode=FindAncestor}}"
            StrokeThickness="1" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="PART_Border" Property="BorderBrush" Value="#666"/>
                    <Setter TargetName="PART_Border" Property="BorderThickness" Value="1"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </eo:BareButton.Template>

</eo:BareButton>

그리드를 사용 하여이 작업을 수행 할 수 있습니다. 다음은 텍스트 상자의 오른쪽 하단에 나타나는 버튼을 만든 방법입니다.

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBox VerticalAlignment="Stretch"  HorizontalAlignment="Stretch" Grid.Row="0" />

        <Button Content="Copy" Width="40" Height="40" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10" Grid.Row="0" />

</Grid>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top