我看過很多例子,如何使用資料模板選擇器根據 x:TargetType 顯示不同的控制元件。我想根據型別別創建一個顯示 RadioButton、TextBox 或 TextBlock 的專案控制元件。我的課可能是這樣的:
public class MyExample<T>
{
public string Name {get;set;}
public Type Type => TypeOf(T)
public T Value {get;set;}
}
我知道 Xaml 無法識別泛型,我不想創建支持泛型的標記擴展,我想保持簡單。我不想為每種型別創建一個具體的類。我知道我可以使用資料觸發器根據屬性(例如型別名稱或型別型別)設定內容模板,但我認為使用資料模板選擇器應該是一種更簡單的方法。我可以在型別屬性上使用 TargetType 而不是型別別嗎?
uj5u.com熱心網友回復:
我可以在型別屬性上使用 TargetType 而不是型別別嗎?
不。
顯而易見的解決方案是DataTemplate為每種型別創建一個子型別和對應的T. 每個子型別的實作都是單行的:
public class MyExampleInt : MyExample<int> { }
public class MyExampleString : MyExample<string> { }
如果您出于某種原因不想創建具體的子型別,則可以使用 aDataTemplateSelector根據 each 的型別選擇模板MyExample<T>:
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
switch (item)
{
case MyExample<int> i:
return IntTemplate;
case MyExample<string> s:
return StringTemplate;
}
return null;
}
uj5u.com熱心網友回復:
這對我有用:
Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1" xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
mc:Ignorable="d"
Background="Red"
d:DataContext="{d:DesignInstance local:ViewModel}"
WindowStartupLocation="CenterScreen"
Title="MainWindow" d:Height="100" d:Width="150" Width="300" Height="300">
<Window.Resources>
<Style x:Key="TextBlockStyle" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center"/>
</Style>
<DataTemplate x:Key="DefaultTemplate">
<TextBlock Text="{Binding Value}" Style="{StaticResource TextBlockStyle}"/>
</DataTemplate>
<DataTemplate x:Key="NumberTemplate">
<syncfusion:UpDown Name="NumericUpdDown" Value="{Binding Value}"/>
</DataTemplate>
<DataTemplate x:Key="TextTemplate">
<TextBlock Text="{Binding Value}" Style="{StaticResource TextBlockStyle}" />
</DataTemplate>
<DataTemplate x:Key="CheckBoxTemplate">
<CheckBox IsChecked="{Binding Value}" HorizontalAlignment="Center"/>
</DataTemplate>
<DataTemplate x:Key="MyDataTemplate">
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<!-- Default Template -->
<Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}" />
<!-- Triggers to change Template -->
<Style.Triggers>
<DataTrigger Binding="{Binding Type.Name}" Value="Int32">
<Setter Property="ContentTemplate" Value="{StaticResource NumberTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Type.Name}" Value="String">
<Setter Property="ContentTemplate" Value="{StaticResource TextTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Type.Name}" Value="Boolean">
<Setter Property="ContentTemplate" Value="{StaticResource CheckBoxTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</Window.Resources>
<ItemsControl
ItemsSource="{Binding Definitions}"
ItemTemplate="{StaticResource MyDataTemplate}">
</ItemsControl>
</Window>
我不想要具體型別類的原因是避免每次添加新型別時添加大量代碼。你覺得這個解決方案怎么樣?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/405559.html
標籤:
下一篇:很難索引陣列
