2011-05-27 11 views
5
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
    xmlns:System_Windows_Controls_Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Toolkit" 
    x:Class="SilverlightApplication5.MainPage" 
    Width="640" Height="480"> 
    <StackPanel x:Name="LayoutRoot" Background="White"> 
     <TextBox x:Name="tbWidth" TextWrapping="Wrap" 
      Text="{Binding Mode=TwoWay, ValidatesOnExceptions=True, Path=RoomWidth}"/> 
     </StackPanel> 
</UserControl> 

RoomWidth - es propiedad.¿Cómo se vincula el cuadro de texto y la propiedad?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace SilverlightApplication5 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 
     private int roomWidth = 10; 
     public int RoomWidth 
     { 
      get { return roomWidth; } 
      set 
      { 
       if (value < 0 || value > 100) 
       { 
        throw new Exception("Data not correct"); 
       } 
       roomWidth = value; 
      } 
     } 

    } 
} 

I need add to Binding source this class. ¿Cómo hacer esto?

+0

No hay suficiente información, ni siquiera ha publicado el encabezado de la clase. –

+0

Debe hacer preguntas a las que usted mismo se sienta seguro respondiendo. No tengo idea de cuál es tu problema. – BentOnCoding

+0

@ H.B y Robotsushi, actualizo la publicación. – Mediator

Respuesta

3

Uso del ElementName por ejemplo:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:System_Windows_Controls_Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Toolkit" 
    x:Class="SilverlightApplication5.MainPage" 
    Width="640" Height="480" 
    Name="control"> 

    <!-- ... --> 
    <TextBox Text="{Binding ElementName=control, Mode=TwoWay, ValidatesOnExceptions=True, Path=RoomWidth}" x:Name="tbWidth" TextWrapping="Wrap"/> 

Si tiene problemas con los enlaces básicos como que sould leer sobre ella. (WPF/Silverlight)

1

DataContext = this;

poner esto en el interior de su constructor.

Cuestiones relacionadas