2010-12-04 58 views
0

La chose que je veux faire semble facile, mais me prend beaucoup de temps sans résultat .. =/ Je voudrais avoir une fenêtre WPF qui prend un objet avec quelques propriétés et utilisé pour peupler un couple d'objet. Dans le spécifique l'objet recieved est défini ainsi:DataBinding entre une interface graphique WPF et un couple de ListBox/CheckBox

public class ParameterForGraphicOptions 
{ 
    public List<VariablesOptions> Variables { get; set; } 
    public List<string> Simulations { get; set; } 
    public List<string> ShowSimulations { get; set; } 
} 
public class VariablesOptions 
{ 
    public string Name { get; set; } 
    public bool Show { get; set; } 
    public bool Average { get; set; } 
    public bool Var { get; set; } 
} 

Et je voudrais remplir 2 ListBox avec des simulations et ShowSimulations, et ont également une autre liste qui est reliée à des variables Nom et 3 case à cocher qui changent leur valeurs lorsque vous modifiez l'élément sélectionné dans la liste .. le code de Windows (.cs) suit:

public GraphicOptions(ParameterForGraphicOptions pfgo) //NAME OF THE WINDOW 
{ 
    InitializeComponent();      //STD CALL 
    this.DataContext = pfgo;     //CONNECTING THE DATA CONTEXT 
} 

le code XAML pour la liaison est la suivante:

<Window x:Class="GUI.GraphicOptions" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="GraphicOptions" Height="450" Width="350"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="25" /> 
      <RowDefinition Height="150"/> 
      <RowDefinition Height="25" /> 
      <RowDefinition Height="150"/> 
      <RowDefinition Height="25"/> 
      <RowDefinition Height="25" /> 
      <RowDefinition Height="11*" /> 
     </Grid.RowDefinitions> 
     <Label Grid.Row="0">Simulation in Progress</Label> 
     <Label Margin="188,0,0,0">Simulation to Show</Label> 
     <ListBox Grid.Row="1" Height="150" HorizontalAlignment="Left" Name="Simulations" VerticalAlignment="Top" Width="140" /> 
     <ListBox Grid.Row="1" Height="150" HorizontalAlignment="Left" Margin="188,0,0,0" Name="SimulationsToShow" VerticalAlignment="Top" Width="140" /> 
     <Label Grid.Row="2">Variables to Show</Label> 

     <ListBox Grid.Column="0" Grid.Row="3" DataContext="{Binding Variables.Name}" Height="150" HorizontalAlignment="Left" Name="Variables" VerticalAlignment="Top" Width="140" Grid.RowSpan="2" /> 
     <CheckBox Grid.Row="3" Content="Show" IsChecked="{Binding Variables.Show}" Height="20" HorizontalAlignment="Left" Name="Show" VerticalAlignment="Top" Width="76" Margin="163,5,0,0" /> 
     <CheckBox Grid.Row="3" Content="Average" IsChecked="{Binding Variables.Average}" Height="25" HorizontalAlignment="Left" Name="Average" VerticalAlignment="Top" Width="76" Margin="174,54,0,0" Checked="Average_Checked" /> 
     <CheckBox Grid.Row="3" Content="Var" IsChecked="{Binding Variables.Var}" Height="25" HorizontalAlignment="Left" Name="Variance" VerticalAlignment="Top" Width="76" Margin="163,31,0,0" /> 
     <CheckBox Content="Refresh Graphic During Computation" Grid.Row="5" Height="25" HorizontalAlignment="Left" Name="Continuos" VerticalAlignment="Top" Width="220" /> 
     <Button Content="Save" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="253,1,0,0" Name="Save" VerticalAlignment="Top" Width="75" Click="Save_Click" /> 
     <Button Content="->" Grid.Row="1" Height="35" HorizontalAlignment="Left" Margin="145,30,0,0" Name="OneSimulation" VerticalAlignment="Top" Width="35" /> 
     <Button Content="=>" Grid.Row="1" Height="35" HorizontalAlignment="Left" Margin="145,65,0,0" Name="AllSimulation" VerticalAlignment="Top" Width="35" /> 
    </Grid> 
</Window> 

J'ai essayé à bien des égards, mais toujours seulement un élément lie, donc je ne comprends pas comment cette bibliothèque fonctionne .. Un grand merci 4 tous: P

La question est: est-il quelque chose de mal dans le code Je posté?

P.S .: Désolé pour mon anglais: P

+0

S'il vous plaît reformuler votre question, il est vraiment difficile de understnad ce que vous voulez demander? – TalentTuner

+0

Je vous suggère de lire au moins un tutoriel sur la liaison de données WPF. http://coredotnet.blogspot.com/2006/05/wpf-data-binding-tutorial.html – VVS

Répondre

1

Ne pas lier un contrôle unique avec plusieurs éléments. Lier avec un seul élément sélectionné.

Voici une solution correcte:

<ListBox x:Name="vars" ItemsSource="{Binding Variables}" DisplayMemberPath="Name" /> 
<CheckBox IsChecked="{Binding SelectedItem.Show, ElementName=vars, Mode=TwoWay}" /> 
<CheckBox IsChecked="{Binding SelectedItem.Average, ElementName=vars, Mode=TwoWay}" /> 
<CheckBox IsChecked="{Binding SelectedItem.Var, ElementName=vars, Mode=TwoWay}" /> 
+0

Merci beaucoup! Maintenant, je vais essayer de résoudre mon problème ^^ " –