2009-04-17 8 views
0

Comment lier deux objets TextBox à System.Windows.Sizestruct? La liaison n'a plus qu'à travailler dans ce sens:Liaison de plusieurs TextBox à une structure dans WPF

(textBox1.Text + textBox2.Text) => (Taille)

Une fois qu'un utilisateur insère la largeur et la hauteur dans les zones de texte sur l'interface utilisateur l'objet des tailles devrait être créé.

XAML:

<TextBox Name="textBox_Width" Text="{Binding ???}" /> 
<TextBox Name="textBox_Height" Text="{Binding ???}" /> 

C#:

private Size size 
{ 
    get; 
    set; 
} 

est-il un moyen facile de le faire?

Édition: La taille est une structure! Par conséquent, "someObject.Size.Width = 123" ne fonctionne pas. Je dois appeler la taille-Constructor et définir someObject.Size = newSize

Répondre

3

Ne pourriez-vous juste exposer 2 propriétés - largeur et hauteur de votre modèle, avec une propriété de taille. La largeur et la hauteur apparaîtront dans vos expressions {Binding}, puis lorsque vous souhaitez obtenir la propriété size, elle s'initialise en fonction de ces deux champs.

Par exemple, votre modèle pourrait être quelque chose comme;

public class MyModel 
{ 
    public int Width{ get; set; } 
    public int Height{ get; set; } 

    public Size Size{ get{ return new Size(Width, Height); }} 
}; 

Espérons que cela aide.

Tony

0

Window1.xaml.cs:

public partial class Window1 : Window 
{ 
    public static readonly DependencyProperty SizeProperty = DependencyProperty.Register("Size", 
     typeof(Size), 
     typeof(Window1)); 

    public Size Size 
    { 
     get { return (Size)GetValue(SizeProperty); } 
     set { SetValue(SizeProperty, value); } 
    } 

    public Window1() 
    { 
     InitializeComponent(); 
     DataContext = this; 
     _button.Click += new RoutedEventHandler(_button_Click); 
    } 

    void _button_Click(object sender, RoutedEventArgs e) 
    { 
     MessageBox.Show(Size.ToString()); 
    } 
} 

Window1.xaml:

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <StackPanel> 
     <TextBox Text="{Binding Size.Width}"/> 
     <TextBox Text="{Binding Size.Height}"/> 
     <Button x:Name="_button">Show Size</Button> 
    </StackPanel> 
</Window> 
Questions connexes