2017-05-29 6 views
-1

Ich habe mehrere Viewmodels. Ich habe drei Datatemplates für drei Listboxen verwendet, die mit 3 Comboxen und ihren ausgewählten Items verknüpft sind. Aber mein Problem ist, dass nur ein Datenkontext perfekt funktioniert. das heißt, wenn ich Code wieWie man mehrere Datenkontexte in XAML anwenden?

public MainWindow() 
     {   
      InitializeComponent(); 
      DataContext = new wbItemViewModel(); 
      DataContext = new IfItemViewModel();       
     } 

<Window.Resources>  

    <DataTemplate x:Key="wbObjectsDataTemplate"> 
     <Grid>    

      <ItemsControl Grid.ColumnSpan="4" Grid.RowSpan="4" Height="24" Width="642" > 
       <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="697" Margin="10,0,0,0" Height="54" > 
        <Label Content="{Binding WBName_lbl}" Margin="0,3,0,5" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2"/>    

        <ComboBox x:Name="wbselect" Margin="5,0,10,1" Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="0"> 
         <ComboBoxItem x:Name="wbstraight" IsSelected="True" Content="straight"></ComboBoxItem> 
         <ComboBoxItem x:Name="wbtapered" Content="tapered"></ComboBoxItem> 
        </ComboBox> 


        <TextBox x:Name="wbDesc" Margin="18,0,20,1" Grid.Column="2" Grid.Row="0"> 
        </TextBox> 

        <Label x:Name="wblengthvalue" Margin="247,0,54,5" FontSize="8" Grid.Row="1" Grid.Column="2"/> 



       </Grid> 
      </ItemsControl> 
      <!--</GroupBox>--> 
     </Grid> 
    </DataTemplate> 

    <DataTemplate x:Key="ifObjectsDataTemplate"> 
     <Grid>    
      <ItemsControl Grid.ColumnSpan="4" Grid.RowSpan="4" Height="24" Width="642" > 
       <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="697" Margin="10,0,0,0" Height="54" >      

        </Grid.RowDefinitions> 

        <Label Content="{Binding IFName_lbl}" Margin="0,3,0,5" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2"/>      

        <ComboBox x:Name="ifselect" Margin="5,0,10,1" Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="0"> 
         <ComboBoxItem x:Name="ifstraight" IsSelected="True" Content="straight"></ComboBoxItem> 
         <ComboBoxItem x:Name="iftapered" Content="tapered"></ComboBoxItem> 
        </ComboBox> 

        <TextBox x:Name="ifDesc" Margin="18,0,20,1" Grid.Column="2" Grid.Row="0"> 
        </TextBox> 

        <Label x:Name="iflengthvalue" Margin="247,0,54,5" FontSize="8" Grid.Row="1" Grid.Column="2"/> 



       </Grid> 
      </ItemsControl>     
     </Grid> 
    </DataTemplate> 

    </Window.Resources> 
    <ComboBox x:Name="wbCombo" ItemsSource="{Binding wbComboBoxList}" SelectedItem="{Binding wbSelectedComboIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="20" Width="92" SelectedIndex="4" Canvas.Left="102" Canvas.Top="19"/>      
          <ComboBox x:Name="ifCombo" ItemsSource="{Binding ifComboBoxList}" SelectedItem="{Binding ifSelectedComboIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="20" Width="92" SelectedIndex="4" 
    Canvas.Left="302" Canvas.Top="19" /> 
    <ListBox x:Name="wbListDataTemplate" ItemsSource="{Binding wbVisibleItems}"   
      ItemTemplate="{DynamicResource wbObjectsDataTemplate}" 
      Background="{x:Null}" 
SelectedItem="{Binding wbSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True" Canvas.Top="51" Height="153" Width="655"> 
          </ListBox> 
    <ListBox x:Name="ifListDataTemplate" ItemsSource="{Binding ifVisibleItems}"    
    ItemTemplate="{DynamicResource ifObjectsDataTemplate}" 
             Background="{x:Null}" 
            SelectedItem="{Binding ifSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
            IsSynchronizedWithCurrentItem="True" Canvas.Top="203" Height="153" Width="655"> 
          </ListBox> 
</Window> 

Nur sichtbar die letzte Datacontext ist geschrieben. Oder wenn ich nur einen einzigen DataContext schreibe, erscheint nur dieser auf meinem Bildschirm. Ich habe verschiedene Lösungen im stackoverflow ausprobiert aber keine Verwendung. Bitte helfen.

Antwort

2

In WPF, Alle UIElement haben immer nur eine DataContext Eigenschaft. Wenn Sie diese Eigenschaft also mehrfach zuweisen, wird der alte Wert überschrieben.

Lösung für Ihr Problem ist, Sie können eine kombinierte Ansicht Modellklasse, die alle drei Sub-ViewModel haben. Danach können Sie dieser Eigenschaft ein eigenes UI-Element zuweisen.

Beispiel:

public class CombinedViewModel 
{ 
    public wbItemViewModel WebItemVM= new wbItemViewModel(); 
    public InnerFlangeViewModel InnerFlangVM= new InnerFlangeViewModel(); 
    public OuterFlangeViewModel OuterVM= new OuterFlangeViewModel();  
} 

public MainWindow() 
{   
    InitializeComponent(); 
    DataContext = new CombinedViewModel(); 
} 

jetzt das alles andere subview Modell Sie es in Ihrem XAML von Eigenschaftsnamen

+1

warum die downvote verwenden kann?! –

+0

Entschuldigung. es hat nicht funktioniert. –

+0

Was ist das Problem mit dem können Sie –

Verwandte Themen