Search code examples
c#wpflistviewcontentcontrol

WPF Styling ContentControl in Listview


I have this style for ContentControl:

<UserControl.Resources>

    <DataTemplate x:Key="textbox">
        <TextBox Text="edit me"/>
    </DataTemplate>
    <DataTemplate x:Key="textblock">
        <TextBlock Text="can't edit"/>
    </DataTemplate>

 <Style x:Key="ContentControlStyle" TargetType="{x:Type ContentControl}">
            <Setter Property="Content" Value="{Binding}"/>
            <Setter Property="ContentTemplate" Value="{StaticResource textblock}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected,RelativeSource={RelativeSource FindAncestor, 
                                             AncestorType={x:Type ListViewItem},AncestorLevel=1}}" 
                                             Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource textbox}" />
                </DataTrigger>
            </Style.Triggers>
  </Style>
</UserControl.Resources>

And that codes:

<ListView.View>
            <GridView  x:Name="UGridview1">
                <GridViewColumn Width=" 90">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ContentControl >
                                
                            </ContentControl>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>

But I want to create the columns dynamically, so I wrote the following codes:

for (x = 0; x <= Lvobj.obj.Length - 1; x++) // ClmnCount - 1
    {
       
        GridViewColumn_ = new GridViewColumn();
        GridViewColumn_.SetValue(NameProperty, "Column" + x);
        GridViewColumn_.Header = Lvobj.obj(x)(clmntxt);
        GridViewColumn_.Width = 99;


        /// This part doesnt work
        ContentControl cntr = new ContentControl();
        cntr.Style = this.Resources("ContentControlStyle");
        ///
       
        GridViewColumn_.CellTemplate = cntr.ContentTemplate;
        UGridview1.Columns.Add(GridViewColumn_);
    }

It never works. What must i do for i can create columns with ContentControl Style?


Solution

  • Either use XamlReader.Parse API with a DynamicResource:

    const string Xaml = @"<DataTemplate " +
        @"xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> " +
        @"<ContentControl Style=""{DynamicResource ContentControlStyle}"" />" +
        "</DataTemplate>";
    DataTemplate DataTemplate_ = System.Windows.Markup.XamlReader.Parse(Xaml) as DataTemplate;
    GridViewColumn_.CellTemplate = DataTemplate_;
    

    Or create a FrameworkElementFactory:

    FrameworkElementFactory cc = new FrameworkElementFactory(typeof(ContentControl));
    cc.SetValue(ContentControl.StyleProperty, this.Resources["ContentControlStyle"]);
    GridViewColumn_.CellTemplate = new DataTemplate() { VisualTree = cc };