2011-07-01 13 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パターンを使用すると、コントロールを直接参照してはいけません。あるいは、さまざまなコントロールを表示するかどうかを決定するブール値プロパティをビューモデルに作成する必要があります。次に、コンバーターを使用して非表示にするコントロールのVisibilityプロパティをこのプロパティにバインドします。

視認性コンバータの詳細については、先にQ/Aをご覧ください。Link

関連する問題