如何在xaml中应用多个datacontext?

时间:2017-05-29 04:42:48

标签: c# .net wpf xaml mvvm

我有多个视图模型。我为三个列表框使用了三个数据窗口,这三个列表框与3个combox及其选定项目绑定。但我的问题是,只有一个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,那么我的屏幕上只会出现一个。我在stackoverflow中尝试了不同的解决方案,但没有用。请帮助。

1 个答案:

答案 0 :(得分:3)

在WPF中,每个glVertex始终只有一个 func uploadProfilePic(){ var data = NSData() data = UIImageJPEGRepresentation(ivProfile.image!, 0.8)! as NSData // set upload path let filePath = "\(userid)" // path where you wanted to store img in storage let metaData = FIRStorageMetadata() metaData.contentType = "image/jpg" self.storageRef = FIRStorage.storage().reference() self.storageRef.child(filePath).put(data as Data, metadata: metaData){(metaData,error) in if let error = error { print(error.localizedDescription) return }else{ //store downloadURL let downloadURL = metaData!.downloadURL()!.absoluteString } } } 属性。因此,如果您多次分配此属性,那么它将覆盖旧值。

您的问题的解决方案是您可以采用一个具有所有三个Sub-ViewModel的组合视图模型类。然后,您可以为此属性分配单个UI元素。

示例:

UIElement

现在这个所有其他子视图模型都可以在属性名称

的xaml中使用它
相关问题