我需要创建两个包含相同数量的项目(动态数量)的控件,第一个控件代表键,第二个代表值。
我需要它,以便当用户调整上列宽度的大小时,它应该影响下一行(值)中的同一列。
这是我想要的一个例子:
<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 完成。注意:它是一个自定义控件,所以我可以在必要时声明适当的属性。但请记住,单元格的高度和宽度必须是动态的,并且对于特定的列/行是独立的。
关于这个问题,我以稍微不同的方式创建它(对单元格有第三个控件),但问题仍然相同,我希望列和单元格的高度宽度是动态的,并赋予用户能力调整它们相互影响的大小。
更新
decyclone 的答案是我很想实现的,但我尝试了他提供的将ItemsControl
s'Grid.IsSharedSizeScope
属性设置为 true 的示例,但它不起作用,这是结果(裁剪):
是否可以在两个不同的控件之间应用共享大小范围?