我有两个使用SharedSizeGroup共享宽度的项控件。 现在的问题是每当我更新文本框内的文本时,请将第二行中的文本设置为999999999&然后删除文本,控件不会重新定位。
这是我的代码: -
XAML: -
<Window x:Class="BindingGroupSample.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindingGroupSample"
Title="Window2" Height="300" Width="300">
<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>
<TextBox Text="{Binding Index}" Background="Gray"/>
</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>
<TextBox Text="{Binding Name}" />
</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.Shapes;
using System.Collections.ObjectModel;
namespace BindingGroupSample
{
public partial class Window2 : Window
{
ObservableCollection<Temp> list1 = new ObservableCollection<Temp>();
ObservableCollection<Temp> list2 = new ObservableCollection<Temp>();
public Window2()
{
InitializeComponent();
for (int i = 0; i < 25; i++)
{
list1.Add(new Temp() { Index = i });
list2.Add(new Temp() { Name = "AA" + i +i });
}
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();
}
}
public class Temp
{
public int Index { get; set; }
public string Name { get; set; }
}
}
请提出建议,无需采取任何活动来刷新控制项目。
实际上我想要实现我的UI,如下所示: -
Item 1 Item 2 Item3 Item 4
Heading 1: AA AA1 AA2 AA3
Heading 2: 20 10 11 89
Heading 3: 10 11 89 7
Heading 4: Expand Expand Expand Expand
此外,从第1项到第4项的水平滚动条始终可见。
答案 0 :(得分:0)
也许你应该采取另一种方式。让我们用所有值填充一个列表,并在ItemTemplate中使用UniformGrid。
C#:
for (int i = 0; i < 25; i++)
{
list1.Add(new Temp()
{
Index = i,
Name = "AA" + i + i
});
}
ItemsControl3.ItemsSource = list1;
XAML:
<ItemsControl Name="ItemsControl3">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<UniformGrid Rows="2">
<TextBox Text="{Binding Index}" Background="Gray" />
<TextBox Text="{Binding Name}" />
</UniformGrid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我希望这就是你所需要的!