【问题标题】:Create a custom PopUp WPF that works for different types创建适用于不同类型的自定义 PopUp WPF
【发布时间】:2018-10-10 18:36:05
【问题描述】:

我是 WPF 的新手,我想知道是否可以创建一个带有 ListBox 的 PopUp 用户控件,该控件可以接受其项目的任何类?

我这里有“Tipo”类的对象列表的 xaml 代码,但我想将此用户控件用于另一个类,比如“Person”类

<UserControl 

...

    <UserControl.Resources>
        <local:BoolToInvertedBoolConverter x:Key="converter"/>
    </UserControl.Resources>


    <Grid x:Name="gridPrincipal">

        <ToggleButton x:Name="tbTipo" Grid.Row="0" Background="{Binding Path=ColorFondo}" Foreground="{Binding Path=ColorTexto}" Opacity="1" Content="{Binding Path=ContenidoToggleButton}" IsEnabled="{Binding ElementName=Popup1, Path=IsOpen, Converter={StaticResource converter}}" />

        <Popup x:Name="Popup1" Grid.Row="0" Grid.Column="1" AllowsTransparency="True"  Placement="MousePoint" Focusable="False" StaysOpen="False" PopupAnimation="Slide" PlacementTarget="{Binding ElementName=tbTipo}" IsOpen="{Binding IsChecked, ElementName=tbTipo, Mode=TwoWay}" >

            <StackPanel Orientation="Vertical" >

                <Grid Margin="0,10,0,0" Background="LightBlue" Height="Auto">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <CheckBox Name="chkTodos" Grid.Column="2" Content="Seleccionar todos" VerticalContentAlignment="Center" MinHeight="25" Margin="0,0,10,0" IsChecked="{Binding SeleccionarTodos}" />
                </Grid>

                <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" MaxHeight="550" MaxWidth="400" Opacity="0.9">
                    <Grid Height="Auto" Width="Auto"  HorizontalAlignment="Center" VerticalAlignment="Stretch">

                        <StackPanel Orientation="Vertical" Width="Auto" HorizontalAlignment="Stretch">

                            <ListBox Name="lbTipo" ItemsSource="{Binding ListTipos, Mode=TwoWay}" Background="LightGray" HorizontalContentAlignment="Stretch" >
                                <ListBox.ItemTemplate>
                                    <DataTemplate DataType="{x:Type local:Tipo}">
                                        <DataTemplate.Resources>
                                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
                                        </DataTemplate.Resources>

                                        <Grid Background="{Binding Path=ColorVigencia}" >
                                            <CheckBox VerticalContentAlignment="Center" IsChecked="{Binding IsChecked}" Focusable="False" Content="{Binding Descripcion}" />
                                        </Grid>


                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </StackPanel>

                    </Grid>
                </ScrollViewer>
            </StackPanel>



        </Popup>


    </Grid>
</UserControl>

【问题讨论】:

  • 我看不到您指定ItemsSource 必须是List&lt;Tipo&gt; 的任何地方。您将遇到的问题是您的按钮绑定到DescripcionIsChecked,它们不是大多数其他类的属性。您可以创建一个接口,说明所有继承它的类都必须具有名为DescripcionIsChecked 的属性。那么它可以安全地用于实现该接口的任何类。
  • 您好,感谢您的回复。我在这里指定:&lt;ListBox ItemsSource="{Binding ListTipos, Mode=TwoWay}" 我认为这就是解决方案,创建一个接口并将其绑定到 ListBox 项,并且我想与控件一起使用的所有类都需要实现我的接口。

标签: c# wpf


【解决方案1】:

我想您可以使用数据模板来做到这一点。这样,您只需为 ListBox 设置 ItemsSource,WPF 就会根据对象类型确定要使用的数据模板。

这是来自https://www.tutorialspoint.com/mvvm/mvvm_wpf_data_templates.htm的示例

<UserControl.Resources> 

   <DataTemplate DataType = "{x:Type data:Student}">

      <StackPanel Orientation = "Horizontal"> 
         <TextBox Text = "{Binding Path = FirstName, Mode = TwoWay}" 
            Width = "100" Margin = "3 5 3 5"/> 

         <TextBox Text = "{Binding Path = LastName, Mode = TwoWay}" 
            Width = "100" Margin = "0 5 3 5"/> 

         <TextBlock Text = "{Binding Path = FullName, Mode = OneWay}" 
            Margin = "0 5 3 5"/> 

      </StackPanel> 

   </DataTemplate> 
</UserControl.Resources>

<Grid> 
   <ListBox ItemsSource = "{Binding Students}"/> 
</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多