简体   繁体   中英

Creating DataTemplate from code behind

Sorry for the not so very general question...

I have a ListView that I have to fill from code behind, and this ListView also need to get its GridViewColumn's from code behind.

For strings it wasn't hard to make the connection, but now I wan't to create a Ellipse that represents a Boolean value in the ListView.

The code in XAML is rather easy, but I fail at converting it to c# code.

Here is parts of the XMAL code:

<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>

And by now I have gotten this far: XAML:

<local:SortableListView >
    <ListView.View>
        <GridView x:Name="GroupListGridView" />
    </ListView.View>
</local:SortableListView>

And in code I have a Collection<GridViewColumn> GridViewColumns that I loop throug and add all items to the GroupListGridView . And I have a function to fill the GridViewColumns collection:

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);
}

I don't think that I'm that far of... Just that I can't figure out those last steps!

The missing lines are:

var ellipseVisBinding = new Binding("isAdmin");
ellipseVisBinding.Converter = new BooleanToVisibilityConverter();

fef.SetBinding(Ellipse.VisibilityProperty, ellipseVisBinding);

(I note that you've excluded the DockPanel in the template from your code version so I've removed that as well)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM