2014-01-26 52 views
0

我需要創建具有相同的事件和邏輯10個或多個文本框的 而不是複製,並粘貼有選項來一次,不是從它繼承創建它嗎? 例如NAME1將從名字繼承的文本框與同一事件

<TextBox x:Name="Name" AcceptsReturn="True" AllowDrop="True" 
     PreviewDragEnter="DropText_PreviewDragEnter" 
     PreviewDrop="DropText_PreviewDrop" 
     SelectionChanged="listbox_SelectionChanged" 
     HorizontalAlignment="Left" Height="25" TextWrapping="Wrap" 
     VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0" 
     Grid.Row="2" /> 

<TextBox x:Name="name1" AcceptsReturn="True" AllowDrop="True" 
     PreviewDragEnter="DropText_PreviewDragEnter" 
     PreviewDrop="DropText_PreviewDrop" 
     SelectionChanged="listbox_SelectionChanged" 
     HorizontalAlignment="Left" Height="25" TextWrapping="Wrap" 
     VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0" 
     Grid.Row="2" /> 
+0

您正在尋找'ItemsControl'。除非你有一個很好的理由不要從WPF UI元素繼承。你在這裏描述的不是。 –

+0

所以只需複製粘貼15個文本框...... :( –

回答

1

繼承正如在評論中提及的HighCore,ItemsControl可能是你的幫助在這裏。

創建一個ItemsControl並將ItemTemplate設置爲在其中包含文本框。 (創建ObjectDataProvider的返回你需要的文本框的數量)

<Grid> 
    <Grid.Resources> 
     <ObjectDataProvider x:Key="EnumerableRange" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        xmlns:linq="clr-namespace:System.Linq;assembly=System.Core" 
        ObjectType="{x:Type linq:Enumerable}" MethodName="Range"> 
       <ObjectDataProvider.MethodParameters> 
        <sys:Int32>1</sys:Int32> 
        <sys:Int32>15</sys:Int32> 
       </ObjectDataProvider.MethodParameters> 
      </ObjectDataProvider> 
    </Grid.Resources> 

    <ItemsControl ItemsSource="{Binding Source={StaticResource EnumerableRange}}"> 
     <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBox x:Name="Name" 
         AcceptsReturn="True" 
         AllowDrop="True" 
         PreviewDragEnter="DropText_PreviewDragEnter" 
         PreviewDrop="DropText_PreviewDrop" 
         SelectionChanged="listbox_SelectionChanged" 
         HorizontalAlignment="Left" Height="25" 
         TextWrapping="Wrap" 
         VerticalAlignment="Top" Width="150" Grid.Column="4" 
         Margin="0,50,0,0" Grid.Row="2"/> 
     </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Grid> 

UPDATE

您可以使用風格,那麼申報那邊的公共屬性和事件,只是有文本框的多個實例指的是這種風格。

<Grid> 
    <Grid.Resources> 
     <Style TargetType="TextBox"> 
      <Setter Property="AcceptsReturn" Value="True"/> 
      <Setter Property="AllowDrop" Value="True"/> 
      <Setter Property="HorizontalAlignment" Value="Left"/> 
      <Setter Property="Height" Value="25"/> 
      <Setter Property="Width" Value="150"/> 
      <Setter Property="TextWrapping" Value="Wrap"/> 
      <Setter Property="VerticalAlignment" Value="Top"/> 
      <Setter Property="Margin" Value="0,50,0,0"/> 
      <EventSetter Event="PreviewDragEnter" 
         Handler="DropText_PreviewDragEnter"/> 
      <EventSetter Event="PreviewDrop" 
         Handler="DropText_PreviewDrop"/> 
      <EventSetter Event="SelectionChanged" 
         Handler="listbox_SelectionChanged"/> 
     </Style> 
    </Grid.Resources> 

    <TextBox x:Name="Name"/> 
    <TextBox x:Name="Name1"/> 
    ...... 
    <TextBox x:Name="Name15"/> 
</Grid> 

注意我還沒有樣式設置x:Key所以默認情況下將獲得適用於您的網格中的所有文本框。如果你不希望設置x:Key和使用,對於所有文本框。

+0

_「ItemsControl你在找什麼」_--沒有看到數據綁定那不是那麼明顯 –

+0

我只是建議他,可能是我應該重新措詞。 –

+0

HI Rohit,我沒有得到爲什麼我們需要的數字,我怎麼也可以打電話給Name2 ... –

2

您可以創建文本框作爲一種資源的控制模板。將資源標記爲x:Shared = False

<Grid.Resources> 
    <ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False"> 
<Grid.Resources> 

在其他文本框實例上使用此模板。

<TextBox x:Name="name1" Grid.Row="0" Template="{StaticResource TxtBoxTemplate}"/> 
    <TextBox x:Name="name2" Grid.Row="1" Template="{StaticResource TxtBoxTemplate}"/> 
    <TextBox x:Name="name3" Grid.Row="2" Template="{StaticResource TxtBoxTemplate}"/> 

完整代碼如下。

<Grid> 
    <Grid.Resources> 
      <ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False"> 
      <TextBox x:Name="Name" 
       AcceptsReturn="True" 
       AllowDrop="True" 
       PreviewDragEnter="DropText_PreviewDragEnter" 
       PreviewDrop="DropText_PreviewDrop" 

       SelectionChanged="listbox_SelectionChanged" 
       HorizontalAlignment="Left" Height="25" TextWrapping="Wrap" 
       VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0" Grid.Row="2" 
        TextChanged="Name_OnTextChanged" 
       /> 
     </ControlTemplate> 
    </Grid.Resources> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <TextBox x:Name="name1" Grid.Row="0" Template="{StaticResource TxtBoxTemplate}"/> 
    <TextBox x:Name="name2" Grid.Row="1" Template="{StaticResource TxtBoxTemplate}"/> 
    <TextBox x:Name="name3" Grid.Row="2" Template="{StaticResource TxtBoxTemplate}"/> 
</Grid> 
0

#第一種方法:創建名稱customTextbox.cs類和寫下的文本框

public class customTextbox:TextBox 
{ 
    public customTextbox():base() 
    { 
     this.AcceptsReturn = true; 
     this.AllowDrop = true; 
     this.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
     //set your remaining propreties 
     this.PreviewDragEnter +=customTextbox_PreviewDragEnter; 
     this.PreviewDragLeave +=customTextbox_PreviewDragLeave; 
     this.SelectionChanged += customTextbox_SelectionChanged;   
    } 

    void customTextbox_SelectionChanged(object sender, System.Windows.RoutedEventArgs e) 
    { 
     //code here 
    } 
    private void customTextbox_PreviewDragLeave(object sender, System.Windows.DragEventArgs e) 
    { 
     //code here 
    } 

    private void customTextbox_PreviewDragEnter(object sender, System.Windows.DragEventArgs e) 
    { 
     //code here 
    }  
} 

,並在XAML

<Window x:Class="WpfApplication3.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:hp="clr-namespace:WpfApplication3"> 

<Grid Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
    </Grid.RowDefinitions> 
    <hp:customTextbox Height="100" Width="100" Grid.Row="0"></hp:customTextbox> 
    <hp:customTextbox Grid.Row="1" ></hp:customTextbox> 
    <hp:customTextbox Grid.Row="2" ></hp:customTextbox> 
</Grid> 

屬性和事件#second方法:創建像這樣(http://prntscr.com/2mren4)一usecontrol並用文本替換用戶控件像下面

<TextBox x:Class="WpfApplication3.CustomTB" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<!--set all properties and event you required inside textbox--> 

和cs文件使用文本框,而不是用戶控件像下面

public partial class CustomTB : TextBox 
{ 
    public CustomTB() 
    { 
     InitializeComponent(); 
    } 
} 

,並在另一個窗口中添加

<Window x:Class="WpfApplication3.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:hp="clr-namespace:WpfApplication3"> 
<Grid Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition>   
    </Grid.RowDefinitions>  
    <hp:CustomTB Grid.Row="0"/> 
</Grid> 

+0

-1這不是winforms。子類化UI元素沒有強大的reaso n顯然是錯誤的。 –

+0

先生我knw這不是winform.and它工作正常wpf also.may你誤解答案。謝謝你 –