2012-06-10 16 views
6

में XAML उपयोगकर्ता नियंत्रण कैसे रखें मेरे पास निम्न main.xaml और usercontrol है।एक ग्रिड

मुझे दूसरी पंक्ति, ग्रिड के दूसरे कॉलम पर उपयोगकर्ता नियंत्रण को कई बार स्थानांतरित करने की आवश्यकता है, दृश्य स्टूडियो का उपयोग करके यह उपयोगकर्ता नियंत्रण को खींचने और छोड़ने की अनुमति नहीं देता है, इसलिए मुझे लगता है कि मुझे इसे कोड द्वारा करना है, मैं बस नहीं जानता कैसे

MainPage.xaml

<Grid HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="1366" x:Name="grid" Background="Black"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="150"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition Width="250"/> 
     </Grid.ColumnDefinitions> 
     <Border BorderBrush="White" BorderThickness="3" Grid.Column="1" Background="Red" CornerRadius="30"/> 
     <TextBlock x:Name="txtCountry" Grid.Column="1" TextWrapping="Wrap" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
     <TextBlock x:Name="txtTime" Grid.Row="1" TextWrapping="Wrap" FontSize="180" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
    </Grid> 

UserControl

<UserControl 
    x:Class="AlarmPro.TimeOnCity" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:AlarmPro" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="150" 
    d:DesignWidth="250"> 

    <Grid Background="Black"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="30"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <Border BorderBrush="#FFDE6A6A" BorderThickness="1" Grid.Row="0" Grid.Column="0" Background="#FFDC4646"> 
      <TextBlock TextWrapping="Wrap" Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"/> 
     </Border> 
     <Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Background="#FFAE4F00"> 
      <TextBlock TextWrapping="Wrap" Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36"/> 
     </Border> 

    </Grid> 
</UserControl> 

उत्तर

8

क्या आपको इसका मतलब है?

<my:UserControlName Grid.Column="2" Grid.Row="2" ... /> 

<my: इस मामले में CLR नाम स्थान UserControl में रहता है के लिए अन्य नाम है। यह संदर्भ के आधार पर <Window> या <UserControl> टैग के अंदर, अपने XAML के शीर्ष पर परिभाषित किया गया है।

उदाहरण के लिए,

<Window ... 
    xmlns:my="clr-namespace:AssemblyName" 
    ... 
/> 
+0

जहाँ मेरा: से आ रही है? –

5

MainPage.Xaml

<Page 
    x:Class="UserControlExample.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:UserControlExample" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid x:Name="MainContent" Background="Azure" > 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Hidden" 
       VerticalScrollBarVisibility="Hidden">       
      <local:UserControl1 x:Name="MyHelloWorldUserControl" Grid.Row="1" /> 
     </ScrollViewer> 
    </Grid>   
</Page> 

UserControl1.xaml

<UserControl 
    x:Class="UserControlExample.UserControl1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:UserControlExample" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="400"> 

    <Grid Background="Bisque"> 
     <StackPanel> 
      <StackPanel Orientation="Horizontal" Height="81"> 
       <TextBlock Text="Your Name is" Foreground="Blue" FontSize="30" Margin="0,0,0,10"/> 
       <TextBox x:Name="Input" Background="White" Width="225" /> 
      </StackPanel> 
      <Button Content="Click Me" Foreground="Brown" FontSize="30" Click="Button_Click"/> 
      <TextBlock x:Name="Output" FontSize="100"/> 
     </StackPanel> 
    </Grid> 
</UserControl> 
+0

लेकिन आपने सबसे अच्छा हिस्सा छोड़ा। (उत्तर: "[नहीं, मेरी ग्रिड की पृष्ठभूमि बिस्क है] (http://seinfeld.wikia.com/wiki/The_Yada_Yada#Quotes)।") – ruffin

2

MainPage.xaml, मैं एक लॉगिन UserControl का उपयोग करके बाध्यकारी हूँ नाम गति: xmlns: यूसी = "clr-namespace: test.Views", क्योंकि मेरे पास फ़ोल्डर में मेरा उपयोगकर्ता नियंत्रण है "विचार"।

<ScrollViewer> 
<UC:Login BtnLoginClick="Login_BtnLoginClick"/> 
</ScrollViewer> 

Login.cs

public partial class Login : UserControl { 

    public event EventHandler BtnLoginClick; 

    public Login() 
    { 
     InitializeComponent(); 
    } 

    private void btnLogin_Click(object sender, RoutedEventArgs e) 
    { 
     string userName = txtUserName.Text; 
     string userPassword = txtUserPassword.Password.Trim(); 
     if (userName != null && userName != string.Empty && userPassword != null &&  userPassword != string.Empty) 
     { 
      if (this.BtnLoginClick != null) 
      { 
       this.BtnLoginClick(this, e); 
      } 
     } 
     else 
     { 
      MessageBox.Show("Invalid username or password"); 
     } 
    } 

}

अंत में, न MainPage.xaml में ईवेंट हैंडलर उपयोग करने के लिए लॉग इन UserControl से बटन क्लिक किया घटना पर कब्जा करने और अन्य गतिविधियाँ करना भूल ।

MainPage.xaml

<UC:Login BtnLoginClick="Login_BtnLoginClick"/> 

यहाँ "BtnLoginClick" अपनी एक घटना हैंडलर Login.xaml [उपयोगकर्ता नियंत्रण] में परिभाषित किया।

इस "BtnLoginClick" घटना के लिए नई घटना बनाएं जैसा कि मैंने "Login_BtnLoginClick" बनाया गया।

MainPage.cs

private void Login_BtnLoginClick(object sender, EventArgs e) 
{ 
Messagebox.Show("Event captured successfully"); 
////Here you can add your stuffs...  
} 
संबंधित मुद्दे