在两个不同的控件中连接电池的宽度/高度?

时间:2010-12-02 05:17:46

标签: wpf binding performance relativesource elementname

我需要创建两个包含相同项目(动态数量)的控件,第一个控件代表键,第二个控件代表值。

我需要它,以便当用户调整上部列宽时,它应该影响下行(值的)中的相同列。

这是我想要的一个例子:

<Window 
  DataContext="{Binding RelativeSource={RelativeSource Self}}"
  x:Class="MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Window.Resources>
    <ItemsPanelTemplate x:Key="ItemsPanelTemplate">
      <VirtualizingStackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
  </Window.Resources>
  <StackPanel>
    <ItemsControl ItemsSource="{Binding Keys}" 
                  ItemsPanel="{StaticResource ItemsPanelTemplate}"/>
    <ItemsControl Grid.Row="1" ItemsSource="{Binding Values}" 
                  ItemsPanel="{StaticResource ItemsPanelTemplate}"/>
  </StackPanel>
</Window>

Imports System.Collections.Specialized
Class MainWindow
  Private Sub Window_Loaded(ByVal sender As Object,
                            ByVal e As RoutedEventArgs) Handles MyBase.Loaded
    DataContext =
      New StringDictionary From
      {
        {"key1", "value1"},
        {"key2", "value2"},
        {"key3", "value3"},
        {"key4", "value4"}
      }
  End Sub
End Class

结果:

同样,我希望能够创建一个类似DataGrid的控件,甚至支持单元格边框,单元格宽度和高度应该连接到其他控件的宽度+允许调整大小。

我更喜欢它做xamly。 注意:它是一个自定义控件,因此我可以在必要时声明适当的属性。但请记住,单元格的高度和宽度必须是动态的,并且必须是特定的列/行。

关于this问题,我以稍微不同的方式创建它(对细胞有第三个控制),但问题仍然是相同的,我想要列和单元格的高度宽度动态,并让用户能够调整它们之间的相互影响。

更新

decyclone's answer是我想要实现的内容,但我尝试了将ItemsControl s'Grid.IsSharedSizeScope属性设置为true的示例,但它不起作用,这里是结果(裁剪):

是否可以在两个不同的控件之间应用共享大小范围?

2 个答案:

答案 0 :(得分:1)

我尝试了一些东西,似乎有效:

XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Window.Resources>
        <local:GroupNameGenerator x:Key="GroupNameGenerator1" />
        <local:GroupNameGenerator x:Key="GroupNameGenerator2" />
    </Window.Resources>
    <Grid>
        <StackPanel Grid.IsSharedSizeScope="True">
            <ItemsControl Name="ItemsControl1">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition SharedSizeGroup="{Binding Converter={StaticResource GroupNameGenerator1}}" />
                            </Grid.ColumnDefinitions>
                            <Border BorderBrush="Black"
                                    BorderThickness="1"
                                    Margin="5"
                                    Padding="5">
                                <TextBlock Text="{Binding}" />
                            </Border>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <ItemsControl Name="ItemsControl2">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition SharedSizeGroup="{Binding Converter={StaticResource GroupNameGenerator2}}" />
                            </Grid.ColumnDefinitions>
                            <Border BorderBrush="Black"
                                    BorderThickness="1"
                                    Margin="5"
                                    Padding="5">
                                <TextBlock Text="{Binding}" />
                            </Border>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </Grid>
</Window>

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<Int32> list1 = new ObservableCollection<Int32>();
        ObservableCollection<String> list2 = new ObservableCollection<String>();

        public MainWindow()
        {
            InitializeComponent();

            for (int i = 0; i < 25; i++)
            {
                list1.Add(i + 1);
                list2.Add(new String('0', ((i + 1) / 3)));
            }

            ItemsControl1.ItemsSource = list1;
            ItemsControl2.ItemsSource = list2;
        }
    }

    public class GroupNameGenerator : IValueConverter
    {
        public Int32 Index { get; set; }

        public GroupNameGenerator()
        {
            Index = 0;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return String.Format("Group{0}", ++Index);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

答案 1 :(得分:0)

您可以查看以下链接中的示例。 sites.google.com/site/html5tutorials/ParallelDataBinding.zip

附加的样本经过列优化。即。,大量列和较少的行数。样本包含50K列和10行。渲染少于一秒。