2011-07-01 74 views
0

我很麻煩。我有列表框控件,其中有很多組合框。無論何時在組合框中選擇值,我都必須將其他控件設置爲隱藏。我正在使用MVVM pattren。我無法從ListBox控件獲取子控件,但我可以在viewmodel中獲取ListBox控件。我如何在viewmodel中獲得這些控件?可能嗎?我正在使用框架4.0。我已經在下面的代碼中顯示了可以寫入的代碼。我無法從使用MVVM的WPF中的ListBox控件獲取子控件

<ListBox x:Name="lstItems" MaxHeight="300" FontSize="11" Margin="12,0,20,38" ItemsSource="{Binding Source={StaticResource listedView}, Path=myItemsSource, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"> 
     <ListBox.ItemTemplate > 
      <DataTemplate> 
       <Border BorderBrush="Blue" Margin="0,4,0,4" BorderThickness="1" CornerRadius="5"> 
        <StackPanel Orientation="Horizontal"> 
         <Label Content="Show rules where:" Name="lblshowrules"></Label> 
         <ComboBox x:Name="cboShowRuleWhere" Height="20" Width="200" ItemsSource="{Binding Source={StaticResource listedView}, Path=FilterRules}" DisplayMemberPath="RuleName" SelectedValuePath="RuleId" SelectedValue="{Binding Source={StaticResource listedView}, Path=SelectedRuleName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ></ComboBox> 
         <Grid Height="29" HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Top" Width="496" Grid.Row="1" Margin="0,0,0,0"> 
          <ComboBox Height="21" HorizontalAlignment="Left" Margin="6,4,0,0" x:Name="cboRuleCondtion" VerticalAlignment="Top" Width="212" /> 
          <TextBox Height="20" HorizontalAlignment="Left" Margin="242,3,0,0" x:Name="txtValue" VerticalAlignment="Top" Width="245" Visibility="Hidden"/> 
          <ComboBox Height="21" HorizontalAlignment="Left" Margin="224,3,0,0" x:Name="cboValue" VerticalAlignment="Top" Width="205" Visibility="Hidden" /> 
          <DatePicker Height="28" HorizontalAlignment="Left" Margin="242,-3,0,0" x:Name="dtpFromDate" VerticalAlignment="Top" Width="98" Visibility="Hidden" /> 
          <DatePicker Height="31" HorizontalAlignment="Left" Margin="346,-3,0,0" x:Name="dtpToDate" VerticalAlignment="Top" Width="98" Visibility="Hidden"/> 
         </Grid> 
         <Button Name="cmdAddLevel" Padding="0" Margin="-1,1,0,-1" Width="75" Command ="{Binding Source={StaticResource listedView}, Path=AddLevelCommand, UpdateSourceTrigger=PropertyChanged}" BorderBrush="White" BorderThickness="1" Height="25" HorizontalContentAlignment="Center"> 
          <StackPanel Orientation="Horizontal"> 
           <Image Height="16" Width="16" HorizontalAlignment="Left" Margin="1,0,0,-1"> 
           </Image> 
           <TextBlock Text="Add Level" FontWeight="Bold" Height="16" Width="70" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="8,2,0,-1" /> 
          </StackPanel> 
         </Button> 
         <Label Name="lblDeleteLevel" Margin="3,0,0,0" Width="75" TabIndex="7" HorizontalAlignment="Left"> 
          <Hyperlink > 
           <TextBlock Text="Delete Level" /> 
          </Hyperlink> 
         </Label> 
        </StackPanel> 
       </Border> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Vertical" /> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
    </ListBox> 

請幫幫我。再一次,我想知道如何從父母控制中獲得子控件...是否有可能?

回答

0

使用MVVM模式,您不應該直接引用任何控件。或者,您應該在視圖模型上創建布爾屬性,以決定是否應該顯示各種控件。然後使用轉換器將要隱藏的控件的可見性屬性綁定到此屬性。

有關可見性轉換器的詳細信息,請參見此前的Q/A:Link