【问题标题】:I unable to get child control from ListBox control in WPF using MVVM我无法使用 MVVM 从 WPF 中的 ListBox 控件获取子控件
【发布时间】:2011-07-01 14:49:04
【问题描述】:

我遇到了严重的麻烦。我有列表框控件,其中有很多组合框。每当在组合框中选择值时,我都必须将其他控件作为隐藏。我正在使用 MVVM 模式。我无法从 ListBox 控件中获取子控件,但我可以在 viewmodel 中获取 ListBox 控件。我怎样才能在视图模型中获得这些控件?可能吗?我正在使用框架 4.0。我已经在下面显示了写在视图中的代码。

  <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>

请帮助我。再次,我想知道如何从父控件中获取子控件...有可能吗?

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    使用 MVVM 模式,您不应直接引用任何控件。或者,您应该在视图模型上创建一个布尔属性,该属性决定各种控件是否应该可见。然后使用转换器将要隐藏的控件的 Visibility 属性绑定到此属性。

    有关可见性转换器的详细信息,请参阅之前的 Q/A:Link

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 1970-01-01
      • 2018-01-31
      • 2015-04-09
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多