英文:
.NET Maui Create a ControlTemplate Programmatically
问题
I hope you can help me since I can't find much information about it, I need to redefine the appearance of the radioButtons, for which I already have the XAML controlTemplate and it works fine. But I need to create a contentView by code (CS) I can't find the way to pass the XAML to C#.
英文:
I hope you can help me since I can't find much information about it, I need to redefine the appearance of the radioButtons, for which I already have the XAML controlTemplate and it works fine. But I need to create a contentView by code (CS) I can't find the way to pass the XAML to c#
<ControlTemplate x:Key="RadioButtonTemplate">
<Border Stroke="#F3F2F1"
StrokeThickness="0"
StrokeShape="RoundRectangle 10"
BackgroundColor="Transparent"
HorizontalOptions="Start"
VerticalOptions="Start">
<VisualStateManager.VisualStateGroups>
<VisualStateGroupList>
<VisualStateGroup x:Name="CheckedStates">
<VisualState x:Name="Checked">
<VisualState.Setters>
<Setter Property="Stroke"
Value="#FF3300" />
<Setter TargetName="check"
Property="Opacity"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unchecked">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="Transparent" />
<Setter Property="Stroke"
Value="#F3F2F1" />
<Setter TargetName="check"
Property="Opacity"
Value="0" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</VisualStateManager.VisualStateGroups>
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Margin="0,0,4,0"
WidthRequest="18"
HeightRequest="18"
HorizontalOptions="End"
VerticalOptions="Start">
<Ellipse Stroke="#63BBCD"
Fill="White"
WidthRequest="16"
HeightRequest="16"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Ellipse x:Name="check"
Fill="#63BBCD"
WidthRequest="16"
HeightRequest="16"
HorizontalOptions="Center"
VerticalOptions="Center" />
</Grid>
<ContentPresenter Grid.Column="1" />
</Grid>
</Border>
</ControlTemplate>
<Style TargetType="RadioButton">
<Setter Property="ControlTemplate"
Value="{StaticResource RadioButtonTemplate}" />
</Style>
Any help is greatly appreciated
答案1
得分: 0
这实际上比你想象的要容易
首先,你需要将资源获取为一个对象,然后将其转换为 ControlTemplate,然后将其分配给所选的 ContentView。
// 如果你的资源是本地的
var rbTemplate = (ControlTemplate)this.Resources["RadioButtonTemplate"];
// 如果你的资源是全局的
var rbTemplate = (ControlTemplate)Application.Current.Resources["RadioButtonTemplate"];
contentView.ControlTemplate = rbTemplate;
英文:
It's actually easier than you might think
You would first need to get the Resource as an object then cast it as ControlTemplate then assign it to the ContentView of choice.
// if your resource is local
var rbTemplate = (ControlTemplate)this.Resources["RadioButtonTemplate"];
//if your resource is global
var rbTemplate = (ControlTemplate)Application.Current.Resources["RadioButtonTemplate"];
contentView.ControlTemplate = rbTemplate;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论