抱歉,这个问题不是很笼统......
我有一个 ListView,我必须从后面的代码中填充,并且这个 ListView 还需要从后面的代码中获取它的 GridViewColumn。
对于字符串,建立连接并不难,但现在我不想在 ListView 中创建一个表示布尔值的 Ellipse。
XAML 中的代码相当简单,但我无法将其转换为 c# 代码。
以下是部分 XMAL 代码:
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="BoolToVisibility" />
<DataTemplate x:Key="templateAdmin">
<DockPanel>
<Ellipse Width="8" Height="8" Visibility="{Binding Path=isAdmin, Converter={StaticResource BoolToVisibility}}" Fill="Black"/>
</DockPanel>
</DataTemplate>
</ResourceDictionary>
<ListView>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding LastName}" Header="Last Name"/>
<GridViewColumn CellTemplate="{StaticResource templateAdmin}"
<GridViewColumnHeader">
<TextBlock Text="S"/>
</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
到目前为止,我已经做到了这一点:XAML:
<local:SortableListView >
<ListView.View>
<GridView x:Name="GroupListGridView" />
</ListView.View>
</local:SortableListView>
在代码中,我有一个Collection<GridViewColumn> GridViewColumns
循环遍历并将所有项目添加到GroupListGridView
. 我有一个功能来填充GridViewColumns
集合:
private void CreateGridViews()
{
//Creating the Text was easy!
GridViewColumns.add(new GridViewColumn(){ Header = "LastName", DisplayMemberBinding = new Binding("LastName") });
//Creating the Ellipse was harder!
GridViewColumn gvc = new GridViewColumn();
DataTemplate dt = new DataTemplate();
gvc.DisplayMemberBinding = new Binding("isAdmin");
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Ellipse));
fef.SetValue(Ellipse.WidthProperty, 8.0D);
fef.SetValue(Ellipse.HeightProperty, 8.0D);
fef.SetValue(Ellipse.FillProperty, new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black));
//I'm guessing that somewhere here there should be some binding to the visibility property and some sort of conversion done... But I can't figure out how!
dt.VisualTree = fef;
gvc.CellTemplate = dt;
GridViewColumns.Add(gvc);
}
我认为我并没有那么远……只是我无法弄清楚最后的步骤!