Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

wpf - Converting ControlTemplate XAML to C#

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 works but I need to convert it to c# as i need it to be dynamic in some cases. If you could help that would be great.

<Setter Property="Template">
<Setter.Value>
    <ControlTemplate TargetType='{x:Type ListViewItem}'>
        <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>
</Setter.Value>

So far I have the following but I am getting errors.

FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
border.SetValue(Border.BackgroundProperty, Brushes.White);
border.SetValue(Border.CornerRadiusProperty, new CornerRadius(8, 8, 8, 8));
border.SetValue(Border.NameProperty, "roundedMask");

As far as I can tell I cant make the VisualBrush as a FrameworkElementFactory (crashes), but if i declare it as a regular element VisualBrush i cant pass border in as a Visual since its a FrameworkElementFactory.

Simply i am getting lost, any help would be appreciated. Thanks for any help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...