sourcecode

WPF 둥근 모서리 텍스트 상자

copyscript 2023. 5. 24. 22:19
반응형

WPF 둥근 모서리 텍스트 상자

저는 WPF를 모르고 지금 배우고 있습니다.저는 둥근 모서리를 찾고 있었습니다.TextBoxWPF에서.그래서 나는 구글을 검색했고 한 조각을 찾았습니다.XAML:

 <!–Rounded Corner TextBoxes–>
<ControlTemplate x:Key=”RoundTxtBoxBaseControlTemplate” TargetType=”{x:Type Control}”>
<Border Background=”{TemplateBinding Background}” x:Name=”Bd” BorderBrush=”{TemplateBinding BorderBrush}”
BorderThickness=”{TemplateBinding BorderThickness}” CornerRadius=”6″>
<ScrollViewer x:Name=”PART_ContentHost”/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property=”IsEnabled” Value=”False”>
<Setter Property=”Background” Value=”{DynamicResource {x:Static SystemColors.ControlBrushKey}}” TargetName=”Bd”/>
<Setter Property=”Foreground” Value=”{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}”/>
</Trigger>
<Trigger Property=”Width” Value=”Auto”>
<Setter Property=”MinWidth” Value=”100″/>
</Trigger>
<Trigger Property=”Height” Value=”Auto”>
<Setter Property=”MinHeight” Value=”20″/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

그래서 이것을 어디에 붙여야 하는지 알려주세요.XAML저를 자세히 도와주세요.저는 WPF 초보자입니다.

@Smolla는 @Daniel Casserly의 답변에 대해 훨씬 더 나은 답변을 했습니다.

<TextBox Text="TextBox with CornerRadius">
  <TextBox.Resources>
    <Style TargetType="{x:Type Border}">
      <Setter Property="CornerRadius" Value="3"/>
    </Style>
  </TextBox.Resources>
</TextBox>

텍스트 상자 및 목록 상자의 모든 테두리에 둥근 모서리를 지정하려면 창 또는 앱에 스타일을 넣으십시오.<Resources>.

WPF에서는 컨트롤의 모양과 느낌을 수정하거나 다시 만들 수 있습니다.예를 들어 텍스트 상자의 모양을 변경한 경우ControlTemplate기존의TextBox코드를 보고 탐색하려면 아래 코드를 사용하십시오.

<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
    <ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}">
        <Border Background="{TemplateBinding Background}" 
                x:Name="Bd" BorderBrush="Black"
                BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10"> 
            <ScrollViewer x:Name="PART_ContentHost"/>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
            <Trigger Property="Width" Value="Auto">
                <Setter Property="MinWidth" Value="100"/>
            </Trigger>
            <Trigger Property="Height" Value="Auto">
                <Setter Property="MinHeight" Value="20"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <TextBox Template="{StaticResource TextBoxBaseControlTemplate}" Height="25" Margin="5"></TextBox>
</Grid>

따라서 창의 리소스 섹션에서 정적 리소스를 선언하고 의 리소스 TextBoxBaseControlTemplate를 사용했습니다.Template의 재산.TextBox~하듯이Template="{StaticResource TextBoxBaseControlTemplate}".

WPF 컨트롤을 사용자 지정하는 템플릿은 아이디어를 얻기 위해 이 문서를 참조합니다.

http://msdn.microsoft.com/en-us/magazine/cc163497.aspx

다음 스타일을 사용하여 모든 텍스트 상자를 둥근 모서리로 변경할 수 있습니다.

<Style TargetType="{x:Type TextBox}">
  <Style.Resources>
    <Style TargetType="{x:Type Border}">
      <Setter Property="CornerRadius" Value="3" />
    </Style>
  </Style.Resources>
</Style>

다음 답변에 영감을 받았습니다. https://stackoverflow.com/a/13858357/3387453

테두리 설정하기텍스트 상자의 두께를 0으로 설정하여 텍스트 상자 주위에 테두리를 추가합니다.

 <Border BorderThickness="1" BorderBrush="Black" CornerRadius="10" Padding="2"
        HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBox Text="Hello ! " BorderThickness="0"/>
 </Border>

출력은 이미지와 같습니다!출력!

이 질문은 msdn: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/549775ed-1c2a-4911-9078-d9c724294fb3/ 에서 잘 논의되었습니다.

거기에 있는 솔루션을 사용해 보십시오. 코드를 어디에 넣어야 하는지 알 수 있을 정도로 매우 상세하고 확실합니다.

연결된 속성을 사용하여 설정할 수 있습니다.TextBox테두리 반지름(버튼에도 동일하게 적용됨).

연결된 속성에 대한 클래스 만들기

public class CornerRadiusSetter
{
    public static CornerRadius GetCornerRadius(DependencyObject obj) => (CornerRadius)obj.GetValue(CornerRadiusProperty);

    public static void SetCornerRadius(DependencyObject obj, CornerRadius value) => obj.SetValue(CornerRadiusProperty, value);

    public static readonly DependencyProperty CornerRadiusProperty =
        DependencyProperty.RegisterAttached(nameof(Border.CornerRadius), typeof(CornerRadius),
            typeof(CornerRadiusSetter), new UIPropertyMetadata(new CornerRadius(), CornerRadiusChangedCallback));

    public static void CornerRadiusChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
    {
        Control control = sender as Control;

        if (control == null) return;

        control.Loaded -= Control_Loaded;
        control.Loaded += Control_Loaded;
    }

    private static void Control_Loaded(object sender, EventArgs e)
    {
        Control control = sender as Control;

        if (control == null || control.Template == null) return;

        control.ApplyTemplate();

        Border border = control.Template.FindName("border", control) as Border;

        if (border == null) return;

        border.CornerRadius = GetCornerRadius(control);
    }
}

그런 다음 첨부된 특성 구문을 사용하여 스타일 중복 없이 여러 텍스트 상자에 스타일을 지정할 수 있습니다.

<TextBox local:CornerRadiusSetter.CornerRadius="10" />
<TextBox local:CornerRadiusSetter.CornerRadius="5, 0, 0, 5" />
<TextBox local:CornerRadiusSetter.CornerRadius="10, 4, 18, 7" />

언급URL : https://stackoverflow.com/questions/4779777/wpf-rounded-corner-textbox

반응형