标签: itemscontrol

如何让 WPF GridSplitter 控件在 ItemsControl 内工作?

谁能向我解释为什么下面的简单示例有效:

<ItemsControl x:Class="UserControl1"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="5" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid Background="Yellow" />

        <GridSplitter Grid.Row="1"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch" />

        <Grid Grid.Row="2"
              Background="Orange" />
    </Grid>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

...但是当我将 ItemsPanelTemplate 设置为主模板时,它不会:

<ItemsControl x:Class="UserControl1"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="5" />
                    <RowDefinition />
                </Grid.RowDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <Grid Background="Yellow" />

    <GridSplitter Grid.Row="1"
                  HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch" />

    <Grid Grid.Row="2" Background="Orange" />
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

它们都在橙色框的顶部显示一个黄色框,它们之间有一个水平分隔线。在第一个示例中,分割器工作正常,允许您调整两个区域的大小。在第二个示例中(生成几乎相同的视觉树),分割器被锁定,它不允许我拖动它来调整两个区域的大小!

这是我想要实现的目标的一个非常简单的示例 - 但它演示了我在实际应用程序中遇到的问题。我一定错过了一些东西,是什么阻止了分离器的功能?所有三个子项都添加到 ItemsPanelTemplate 网格中,确定......

任何解释或修复将不胜感激!

问候,戴夫

itemscontrol gridsplitter

1
推荐指数
1
解决办法
1531
查看次数

如何在XAML中设置项目的高度,以便它们在父ItemsControl中始终占用相同比例的可用空间?

我有一个带有以下ItemTemplate的ItemsControl:

<DataTemplate x:Key="myItemTemplate">
    <TextBlock Height="???" Text="{Binding Path=Description}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何在模板中设置TextBlock的高度,以便自动假定ItemsControl.Height div ItemsCount垂直空间量?

当只有一个项目时,我希望它是容器的整个高度,当有两个时,每个应该是一半大小,依此类推.

如果可能的话,我宁愿在XAML中完全这样做,以保持我的ViewModel清除UI逻辑.

wpf xaml itemscontrol

1
推荐指数
1
解决办法
4357
查看次数

如何使ItemsControl根据运行时类型选择不同的模板

我有一个xaml页面,上面有一个ItemsControl控件.ItemsControl绑定到ObservableCollection的Guests.来宾集合可以包含两种不同类型的对象:USGuest和UKGuest,两者都继承自Guest.是否可以为ItemsControl创建两个(或更多)模板,并根据集合中当前项的运行时类型自动选择它们?

silverlight wpf itemscontrol mvvm

1
推荐指数
1
解决办法
3640
查看次数

如何在ItemControl中定位ViewModel

我的主窗口ViewModel有一个ViewModel的ObservableCollection,名为ViewModels.

Mainwindow XAML有一个ItemsControl,其ItemsSource绑定到ViewModels.

当我有

<ItemsControl ItemsSource="{Binding ViewModels}" />
Run Code Online (Sandbox Code Playgroud)

与集合中的每个ViewModel关联的视图将一个在另一个下方呈现.视图是UserControls,显示dataGrids.

如何以可自定义的方式定位它们,例如VM1位于左侧,VM2和VM3堆叠在VM1右侧的另一个之上.

每个VieModel都有PosX,PosY,Width和Height属性,我一直在尝试各种模板方法但到目前为止还没有成功.

我已经找到了如何使用Observable图像集合的示例,但我正在努力的一件事是我的集合是ViewModels.

wpf templating itemscontrol wpf-positioning

1
推荐指数
1
解决办法
2574
查看次数

WPF:ItemsControl的边框不可见

我跟着指示此博客添加ScrollIntoViewItemsControl.

但这使得边界不可见:

<ItemsControl BorderBrush="Black"
              BorderThickness="3">
    <ItemsControl.Template>
        <ControlTemplate>
            <ScrollViewer Padding="{TemplateBinding Padding}">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
    <TextBlock Text="Test" />
    <TextBlock Text="Test" />
    <TextBlock Text="Test" />
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

为了显示边框,我必须删除:

<ItemsControl.Template>
    <ControlTemplate>
        <ScrollViewer Padding="{TemplateBinding Padding}">
            <ItemsPresenter />
        </ScrollViewer>
    </ControlTemplate>
</ItemsControl.Template>
Run Code Online (Sandbox Code Playgroud)

但这样我就无法使用该ScrollIntoView方法.

有任何想法吗?谢谢

wpf itemscontrol wpf-controls

1
推荐指数
1
解决办法
2035
查看次数

为什么ItemsControl.ToolTip仅在鼠标悬停在其项目上时才可见

这里的问题.我有一个ItemsControl,如果用户将鼠标悬停在ItemsControl上,我想显示一个工具提示.看起来很简单吧?

这是一个例子:

<ItemsControl BorderBrush="Blue" BorderThickness="1">
    <ItemsControl.ToolTip>
        <ToolTip Content="Text" />
    </ItemsControl.ToolTip>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <Label BorderBrush="Red" BorderThickness="1">One</Label>
    <Label BorderBrush="Red" BorderThickness="1">Two</Label>
    <Label BorderBrush="Red" BorderThickness="1">Three</Label>
    <Label BorderBrush="Red" BorderThickness="1">Four</Label>
    <Label BorderBrush="Red" BorderThickness="1">Five</Label>
    <Label BorderBrush="Red" BorderThickness="1">Six</Label>
    <Label BorderBrush="Red" BorderThickness="1">Seven</Label>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

使窗口宽度足够小,让WrapPanel包装一个项目.并尝试将鼠标悬停在ItemsControl上(ToolTip不会出现),而不是将鼠标悬停在Label上(将出现ToolTip).

为什么这种行为是正确的,以及如何在这种情况下强制使用ToolTip

wpf xaml mouseover tooltip itemscontrol

1
推荐指数
1
解决办法
1152
查看次数

StackPanel方向在ItemsControl上不完全水平

我有以下问题.我正在使用Items控件,我不明白为什么我的结果是在几行而不是在一行上.这是我的代码:

<StackPanel Orientation="Horizontal" Margin="10,10,10,10">
    <ItemsControl ItemsSource="{Binding Countries}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate x:Name="TabCountries" >
                <StackPanel Orientation="Horizontal">
                    <TextBlock Name="Country" Text="{Binding nom}" Style="{StaticResource whiteFontColor}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                    <CheckBox Margin="0,0,5,0" Name="isCountryAllowed" IsChecked="{Binding isAllowed}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

FR
BE
AN
Run Code Online (Sandbox Code Playgroud)

而不是那样:

FR BE AN ...
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?

wpf layout xaml itemscontrol stackpanel

1
推荐指数
1
解决办法
1090
查看次数

如何在ListBox.ItemsSource的2个数据源之间切换

将ItemsControl.ItemsSource绑定到给定属性上的2个不同源的最佳/优雅方法是什么?

绑定只应该对2个集合中的一个进行,ItemsControl绑定到的集合的选择应该基于某个属性.

我有一个绑定到ViewModel的View.我想要绑定的集合位于该ViewModel下的不同层次结构路径中.

我有一个基于MultiBinding的解决方案,但我认为应该有更优雅的解决方案.

<CollectionViewSource x:Key="CVS">
      <CollectionViewSource.Source  >
          <MultiBinding Converter="{StaticResource myMultiBindingConverter}">
              <Binding  Path="XXXX.YYYY.ObservableCollection1"  />
              <Binding Path="XXXX.ObservableCollection2" />                    
          </MultiBinding>
      </CollectionViewSource.Source>                            
</CollectionViewSource>

<ListBox  x:Name="myListBox"                                   
          ItemsSource="{Binding Source={StaticResource CVS}}" />
Run Code Online (Sandbox Code Playgroud)

转换器:

public class myMultiBindingConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {

        foreach (var item in values)
        {
            if(myDependecyProperty == getFirstCollection)
            {
              //make sure the item is of first collection type based on its item property
              return item;
             }
            else
            {
              //make sure the …
Run Code Online (Sandbox Code Playgroud)

wpf binding itemscontrol multibinding

1
推荐指数
1
解决办法
1390
查看次数

ItemsControl忽略参考资料中的DataTemplates

我想将多个类型的集合绑定到在画布中显示的ItemsControl.到目前为止,我的代码如下:

<ItemsControl ItemsSource="{Binding Path=Objects}">
     <ItemsControl.Resources>
         <DataTemplate DataType="self:CalibrationRectangle">
             <Rectangle Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Width="{Binding Width}" Height="{Binding Height}" />
         </DataTemplate>
         <DataTemplate DataType="self:PolygonVM">
             <Polygon Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Points="{Binding Points}" />
         </DataTemplate>
     </ItemsControl.Resources>
     <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
             <Canvas Background="Red" />
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     <!--<ItemsControl.ItemTemplate>
         <DataTemplate DataType="self:CalibrationRectangle">
             <Rectangle Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Width="{Binding Width}" Height="{Binding Height}" />
         </DataTemplate>
     </ItemsControl.ItemTemplate>-->
     <ItemsControl.ItemContainerStyle>
         <Style>
             <Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
             <Setter Property="Canvas.Left" Value="{Binding Path=X}" />
         </Style>
     </ItemsControl.ItemContainerStyle>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

在此代码状态中,资源中的DataTemplates被完全忽略,而对象则表示为字符串.相反,我使用注释掉的行并在ItemsControl.ItemsTemplate中定义DataTemplate它的工作原理.但是,由于我需要多个DataTemplates(每种类型一个),这不是解决方案.

这里有什么问题?为什么资源中的DataTemplates不起作用?

任何想法都赞赏:)

c# wpf xaml datatemplate itemscontrol

1
推荐指数
1
解决办法
1163
查看次数

C# WPF:带有添加内容按钮的 ItemsControl

我想用一个按钮来实现一个 ItemsControl,该按钮向另一个 ViewModel 添加相同的内容。到目前为止,我有这个代码:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Controls:ItemView />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

该按钮应该始终是控件中的最后一项,并且应该只有一个添加按钮。有没有人有好的解决方案?我可以用丑陋的解决方法自己做,但我讨厌丑陋的解决方法:)

c# wpf itemscontrol

1
推荐指数
1
解决办法
1237
查看次数