2017-05-29 26 views
-1

我有多個viewmodels。我使用了三個數據模板,用於三個列表框,這三個列表框用3個comboxes及其選定的項目綁定。但我的問題是,只有一個數據上下文完美地工作。即,如果 我寫像如何在xaml中應用多個datacontext?

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> 

代碼只有最後的DataContext是可見的。或者,如果我只寫一個DataContext,那麼只有那一個出現在我的屏幕上。我已經嘗試了不同的解決方案,但沒有用。請幫助。

回答

2

在WPF中,每個UIElement總是隻有一個DataContext屬性。所以如果你多次分配這個屬性,那麼它將覆蓋舊的值。

針對您的問題的解決方案您可以採用一個組合視圖模型類,它具有全部三個子視圖模型。然後你可以分配這個屬性單獨的UI元素。

例子:

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

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

現在這個可以通過屬性名稱中使用它在您的XAML的所有其他子視圖模型

+1

爲什麼downvote?! –

+0

對不起。它沒有工作。 –

+0

什麼是問題,你可以指定 –

相關問題