我有一个2维对象的数组,我基本上要数据绑定每一个细胞在WPF网格。目前我有这个工作,但我做的大部分程序上。我创建了正确数量的行和列的定义,那么我通过细胞循环,并创建控件并设置正确的绑定的每一个。
至少我想是能够使用模板来指定XAML的控件和绑定。理想情况下,我想摆脱的程序code,只是做这一切与数据绑定,但我不知道这是可能的。
下面是我目前使用code:
公共无效BindGrid()
{
m_Grid.Children.Clear();
m_Grid.ColumnDefinitions.Clear();
m_Grid.RowDefinitions.Clear();
为(中间体X = 0 X - 其中; MefGrid.Width; X ++)
{
m_Grid.ColumnDefinitions.Add(新ColumnDefinition(){宽度=新GridLength(1,GridUnitType.Star)});
}
对于(INT Y = 0; Y< MefGrid.Height; Y ++)
{
m_Grid.RowDefinitions.Add(新RowDefinition(){高度=新GridLength(1,GridUnitType.Star)});
}
为(中间体X = 0 X - 其中; MefGrid.Width; X ++)
{
对于(INT Y = 0; Y< MefGrid.Height; Y ++)
{
细胞细胞=(细胞)MefGrid [X,Y];
的SolidColorBrush画笔=新的SolidColorBrush();
VAR结合=新的绑定(开);
binding.Converter =新BoolColorConverter();
binding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(刷,SolidColorBrush.ColorProperty,结合);
VAR RECT =新的Rectangle();
rect.DataContext =细胞;
rect.Fill =刷;
rect.SetValue(Grid.RowProperty,Y);
rect.SetValue(Grid.ColumnProperty中,x);
m_Grid.Children.Add(RECT);
}
}
}
解决方案
网格的目的不是为了真正的数据绑定,它仅仅是一个面板。我列出下来的最简单的方法来完成二维表的可视化
< Window.Resources>
< DataTemplate中X:关键=DataTemplate_Level2>
<按钮内容={结合}高度=40WIDTH =50保证金=4,4,4,4/>
< / DataTemplate中>
< DataTemplate中X:关键=DataTemplate_Level1>
< ItemsControl中的ItemsSource ={结合}的ItemTemplate ={DynamicResource DataTemplate_Level2}>
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的方向=横向/>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel>
< / ItemsControl的>
< / DataTemplate中>
< /Window.Resources>
<电网>
< ItemsControl的X:名称=乐善堂的ItemTemplate ={DynamicResource DataTemplate_Level1}/>
< /网格>
而在code后面设置LST的的ItemsSource与TwoDimentional数据结构。
公共窗口1()
{
名单<列表< INT>> LSTS =新的名单,其中,名单,其中,INT>>();
的for(int i = 0;我小于5;我++)
{
lsts.Add(新名单,其中,INT>());
为(诠释J = 0; J&小于5; J ++)
{
LSTS [I]。新增(I * 10 + J);
}
}
的InitializeComponent();
lst.ItemsSource = LSTS;
}
这为您提供了以下屏幕输出。您可以编辑DataTemplate_Level2到对象的添加更具体的数据。
I have a 2-dimensional array of objects and I basically want to databind each one to a cell in a WPF grid. Currently I have this working but I am doing most of it procedurally. I create the correct number of row and column definitions, then I loop through the cells and create the controls and set up the correct bindings for each one.
At a minimum I would like to be able to use a template to specify the controls and bindings in xaml. Ideally I would like to get rid of the procedural code and just do it all with databinding, but I'm not sure that's possible.
Here is the code I am currently using:
public void BindGrid()
{
m_Grid.Children.Clear();
m_Grid.ColumnDefinitions.Clear();
m_Grid.RowDefinitions.Clear();
for (int x = 0; x < MefGrid.Width; x++)
{
m_Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star), });
}
for (int y = 0; y < MefGrid.Height; y++)
{
m_Grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star), });
}
for (int x = 0; x < MefGrid.Width; x++)
{
for (int y = 0; y < MefGrid.Height; y++)
{
Cell cell = (Cell)MefGrid[x, y];
SolidColorBrush brush = new SolidColorBrush();
var binding = new Binding("On");
binding.Converter = new BoolColorConverter();
binding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(brush, SolidColorBrush.ColorProperty, binding);
var rect = new Rectangle();
rect.DataContext = cell;
rect.Fill = brush;
rect.SetValue(Grid.RowProperty, y);
rect.SetValue(Grid.ColumnProperty, x);
m_Grid.Children.Add(rect);
}
}
}
解决方案
The purpose of the Grid is not for real databinding, it is just a panel. I am listing down the easiest way to accomplish the visualization of a two dimensional list
<Window.Resources>
<DataTemplate x:Key="DataTemplate_Level2">
<Button Content="{Binding}" Height="40" Width="50" Margin="4,4,4,4"/>
</DataTemplate>
<DataTemplate x:Key="DataTemplate_Level1">
<ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_Level2}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
</Window.Resources>
<Grid>
<ItemsControl x:Name="lst" ItemTemplate="{DynamicResource DataTemplate_Level1}"/>
</Grid>
And in the code behind set the ItemsSource of lst with a TwoDimentional data structure.
public Window1()
{
List<List<int>> lsts = new List<List<int>>();
for (int i = 0; i < 5; i++)
{
lsts.Add(new List<int>());
for (int j = 0; j < 5; j++)
{
lsts[i].Add(i * 10 + j);
}
}
InitializeComponent();
lst.ItemsSource = lsts;
}
This gives you the following screen as output. You can edit the DataTemplate_Level2 to add more specific data of your object.
相关推荐
最新文章