Converting ControlTemplate XAML to C#

前端 未结 2 413
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 18:30

I have been stumped with trying to convert the following code into pure c#. This XAML code is from Cavanaghs blog on how to make rounded corners on anything. The code work

相关标签:
2条回答
  • 2020-12-19 19:18

    You don't actually have to convert this into C# to apply it dynamically. If you add it to your application resources, within your App.xaml file as follows:

    <Application.Resources>
        <ControlTemplate TargetType='{x:Type ListViewItem}' x:Key="MyListViewItemTemplate">
            <Grid>
                <Border CornerRadius="15" Name="mask" Background="White"/>
                <StackPanel Background="Beige">
                    <StackPanel.OpacityMask>
                        <VisualBrush Visual="{Binding ElementName=mask}"/>
                    </StackPanel.OpacityMask>
                    <GridViewRowPresenter Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}"/>
                    <TextBlock Background="LightBlue" Text="{Binding News}" />
                </StackPanel>
            </Grid>
        </ControlTemplate>
    </Application.Resources>
    

    Note the x:Key attribute which keys this item.

    You can then look it up anywhere in your code ...

    ControlTemplate template = this.Findresource("MyListViewItemTemplate") as ControlTemplate
    

    You can then apply it as and when you need it!

    0 讨论(0)
  • 2020-12-19 19:28

    You do not want to know this. Seriously, you don't, it's a nightmare.

    Edit: If i did not make any mistake this is the translation of your code...

    Setter setter = new Setter();
    setter.Property = ListViewItem.TemplateProperty;
    ControlTemplate template = new ControlTemplate(typeof(ListViewItem));
    var grid = new FrameworkElementFactory(typeof(Grid));
    var border = new FrameworkElementFactory(typeof(Border));
    border.SetValue(Border.BackgroundProperty, Brushes.White);
    border.SetValue(Border.NameProperty, "mask");
    border.SetValue(Border.CornerRadiusProperty, new CornerRadius(15));
    grid.AppendChild(border);
    var stackPanel = new FrameworkElementFactory(typeof(StackPanel));
    stackPanel.SetValue(StackPanel.BackgroundProperty, Brushes.Beige);
    var visualBrush = new FrameworkElementFactory(typeof(VisualBrush));
    visualBrush.SetBinding(VisualBrush.VisualProperty, new Binding() { ElementName = "mask" });
    stackPanel.SetValue(StackPanel.OpacityMaskProperty, visualBrush);
    var gridViewRowPresenter = new FrameworkElementFactory(typeof(GridViewRowPresenter));
    gridViewRowPresenter.SetValue(GridViewRowPresenter.ContentProperty, new TemplateBindingExtension(GridViewRowPresenter.ContentProperty));
    gridViewRowPresenter.SetValue(GridViewRowPresenter.ColumnsProperty, new TemplateBindingExtension(GridView.ColumnCollectionProperty));
    stackPanel.AppendChild(gridViewRowPresenter);
    var textBlock = new FrameworkElementFactory(typeof(TextBlock));
    textBlock.SetValue(TextBlock.BackgroundProperty, Brushes.LightBlue);
    textBlock.SetBinding(TextBlock.TextProperty, new Binding("News"));
    stackPanel.AppendChild(textBlock);
    grid.AppendChild(stackPanel);
    template.VisualTree = grid;
    setter.Value = template;
    

    Edit: There is still a bug left, the VisualBrush cannot be created like that, the rest seems to work.

    0 讨论(0)
提交回复
热议问题