2014-01-18 4 views
2

인스턴트 메신저 텍스트 상자를 많이 사용하고 옆에 일반적인 많은 레이블을 사용하여 내 질문은 복사 붙여 넣기 대신 wpf 경우입니까? 나는 모든 텍스트 상자가 같은 행동을 가지고 원하는 다음 텍스트 상자 대신 이름과 화면 에 장소가있는 경우텍스트 상자 및 레이블 상속 사용

예를 들어

<TextBox x:Name="name2" 
        AcceptsReturn="True" 
        AllowDrop="True" 
        PreviewDragEnter="DropText_PreviewDragEnter" 
        PreviewDrop="DropText_PreviewDrop" 
        PreviewDragOver="DropText_PreviewDragOver" 

        HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" 
        VerticalAlignment="Top" Width="80" Grid.Column="4" Margin="4,50,0,0" Grid.Row="2" 
        /> 

     <TextBox x:Name="name1" 
        AcceptsReturn="True" 
        AllowDrop="True" 
        PreviewDragEnter="DropText_PreviewDragEnter" 
        PreviewDrop="DropText_PreviewDrop" 
        PreviewDragOver="DropText_PreviewDragOver" 

        HorizontalAlignment="Left" 
        Height="20" 
        TextWrapping="Wrap" 
        Text="" VerticalAlignment="Top" Width="80" Grid.Column="4" Margin="4,75,0,0" Grid.Row="2"/> 
+0

스타일 사용. 예 : [WPF 가이드 투어] (http://www.codeproject.com/Articles/18388/A-Guided-Tour-of-WPF-Part-5-Styles) 또는 [WPF 비디오의 스타일 사용] (http : /kr.youtube.com/watch?v=CBs5eEsIk3A). – LPL

답변

1

당신은 모두를 저장합니다, Style을 사용할 수 있습니다 컨트롤의 설정 :

<Style TargetType="{x:Type TextBox}"> 
    <Setter Property="AcceptsReturn" Value="True" /> 
    <Setter Property="AllowDrop" Value="True" /> 
    ... 
    <Setter Property="Margin" Value="4,75,0,0" /> 
</Style> 

스타일이 키를 정의하면 명시 적으로 나타내는 컨트롤에만 적용됩니다. 예 :

<Style x:Key="TextBoxOneStyle" TargetType="{x:Type TextBox}"> 
    <Setter Property="AcceptsReturn" Value="False" /> 
    <Setter Property="AllowDrop" Value="True" /> 
    ... 
    <Setter Property="Margin" Value="4,0,0,0" /> 
</Style> 

<Style x:Key="TextBoxTwoStyle" TargetType="{x:Type TextBox}"> 
    <Setter Property="AcceptsReturn" Value="True" /> 
    <Setter Property="AllowDrop" Value="True" /> 
    ... 
    <Setter Property="Margin" Value="4,75,0,0" /> 
</Style> 

사용 :

<TextBox Name="TextBoxOne" 
     Style="{StaticResource TextBoxOneStyle}" /> 

<TextBox Name="TextBoxTwo" 
     Style="{StaticResource TextBoxTwoStyle}" /> 

또한 EventSetter를 통해 이벤트 핸들러를 지정할 수 있습니다

Styling and Templating MSDN

:

<Style TargetType="{x:Type TextBox}"> 
    <EventSetter Event="PreviewDragEnter" Handler="DropText_PreviewDragEnter" /> 
</Style> 

자세한 내용은이 링크를 참조하시기 바랍니다

관련 문제