Listview 中的 WPF 样式 ContentControl

WPF Styling ContentControl in Listview

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>

那个代码:

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

但是我想动态创建列,所以我写了下面的代码:

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

它永远行不通。我必须做什么才能创建具有 ContentControl 样式的列?

使用 XamlReader.Parse API 和 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_;

或创建 FrameworkElementFactory:

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

用方括号访问ResourceDictionary(这是一个Dictionary):

this.Resources["ContentControlStyle"]

确保在调用 UserControl.OnInitialized 之前不要查找样式。您可以在 UserControl 中覆盖 OnInitialized,然后初始化 ListView。或者处理 Loaded 事件。

或者(推荐),考虑定义样式 implicit(没有 x:key)。一旦加载目标,样式将自动应用。这样你就不用去处理资源了。
您可以通过在 ListView:

ResourceDictionary 中定义它来限制范围
<ListView>
  <ListView.Resources>
    <Style 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>
  </ListView.Resources>

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