2016-07-09 9 views
1

Comment puis-je définir la valeur de CornerRadious valeur de modèle de contrôle pris de la définition de contrôle personnalisé ... S'il vous plaît aider XAML pour le contrôle de l'utilisateur estvaleurs de passage au contrôle propriété de dépendance du modèle

<UserControl x:Class="Biz10.RoundedCornerTextBox" 
       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" 
       xmlns:local="clr-namespace:Biz10" 
       mc:Ignorable="d" 
       d:DesignHeight="300" d:DesignWidth="300"> 
     <UserControl.Resources> 
      <ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}"> 
       <Border Background="{TemplateBinding Background}" 
        x:Name="Bd" BorderBrush="Gray" 
        BorderThickness="1" **CornerRadius="5"**> 
        <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> 
     </UserControl.Resources> 
     <Grid> 
      <TextBox Template="{StaticResource TextBoxBaseControlTemplate}" Height="25" Margin="5"></TextBox> 
     </Grid> 
    </UserControl> 

fichier C# Code est

using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 

using System.Windows.Shapes; 

namespace Biz10 
{ 
    /// <summary> 
    /// Interaction logic for CornerTextBox.xaml 
    /// </summary> 
    public partial class RoundedCornerTextBox : UserControl 
    { 
     public static readonly DependencyProperty _cornrerRadious= DependencyProperty.Register("CornerRadious", typeof(int), typeof(RoundedCornerTextBox)); 
    public RoundedCornerTextBox() 
    { 
     InitializeComponent(); 
    } 
    public int CornerRadious 
    { 
     get 
     { 
      return (int)GetValue(_cornrerRadious); 
     } 
     set 
     { 
      SetValue(_cornrerRadious, value); 
     } 
    } 
} 

}

Je veux déclarer par

un contrôle personnalisé dans la fenêtre
<custome:RoundedCornerTextBox CornerRadious="7" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="7" x:Name="txtAccountName" ></custome:RoundedCornerTextBox> 

Est-il possible

+0

Oui. C'est évidemment possible. – ViVi

Répondre

0

Vous devez changer la Template de votre UserControl comme ci-dessous:

<UserControl.Template> 
    <ControlTemplate TargetType="UserControl"> 
     <Border BorderThickness="3" BorderBrush="#FFF0B0B0" CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource AncestorType=UserControl}}"> 
      <Grid> 
       <TextBox Width="100"/> 
      </Grid> 
     </Border> 
    </ControlTemplate> 
</UserControl.Template> 

Voyez comment la propriété est CornerRadius binded.

0

Vous n'avez pas besoin de UserControl. Il suffit de faire un contrôle personnalisé (http://www.wpftutorial.net/howtocreateacustomcontrol.html) a appelé RoundedCornerTextBox que

  1. étend TextBox
  2. change de modèle à ce que vous avez besoin
  3. expose une nouvelle propriété de dépendance CornerRadius et enfin
  4. utilise TemplateBinding pour connecter cette propriété la bordure à l'intérieur de votre modèle.