我是 WPF 的新手,花了數小時尋找解決方案,但我找不到任何東西。
我想顯示一個類的所有屬性DataTemplate:
<ItemsControl ItemsSource="{Binding Car}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public class Car
{
public int Axes { get; set; }
public int Seats { get; set; }
public int Doors { get; set; }
public VehicleProperties.Car.Type Type { get; set; }
public int MaxWeight { get; set; }
public Fuel Fuel { get; set; }
public double ConsumptionPer100km { get; set; }
public bool Childseat { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public int FirstRegistration { get; set; }
public int Mileage { get; set; }
public Transmission Transmission { get; set; }
public int Power { get; set; }
public bool Tempomat { get; set; }
public SolidColorBrush Color { get; set; }
public int PollutionClass { get; set; }
public int EnvironmentalBadge { get; set; }
}
我怎樣才能至少顯示名稱并正確系結ItemsSource和TextBlock?我需要那個,因為在此之后,我將設計DataTemplate帶有DataType屬性的s 。
uj5u.com熱心網友回復:
如果您只對具體型別的屬性名稱感興趣,則可以創建一個自定義標記擴展來接收型別并使用反射來獲取其屬性資訊并將屬性名稱作為可列舉回傳。
public class PropertyNamesExtension : MarkupExtension
{
public Type Type { get; set; }
public PropertyNamesExtension(Type type)
{
Type = type;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Type?.GetProperties().Select(propertyInfo => propertyInfo.Name);
}
}
在 XAML 中,您可以使用型別Car為ItemsSource.
<ItemsControl ItemsSource="{local:PropertyNamesExtension local:Car}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如果您需要更多資訊,例如每個屬性的型別,請回傳PropertyInfos。
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Type?.GetProperties();
}
<ItemsControl ItemsSource="{local:PropertyNamesExtension local:Car}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Name, Mode=OneWay}"/>
<Run Text="{Binding PropertyType, Mode=OneWay}"/>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我需要它,因為在此之后,我將設計帶有 DataType-Property 的 DataTemplates。
我不知道您的確切目標是什么,但我認為您正在努力實作不同的目標。讓我們假設你想顯示單個實體的Car。然后通過屬性暴露該實體,并將其系結為Content一個ContentControl具有DataTemplate。
public Car Car { get; }
<ContentControl Content="{Binding Car}">
<ContentControl.ContentTemplate>
<DataTemplate DataType="{x:Type local:Car}">
<StackPanel>
<TextBlock Text="{Binding Brand}"/>
<TextBlock Text="{Binding Axes}"/>
<!-- ...other markup. -->
</StackPanel>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
If you want to show a list of Cars, expose them as any collection, e.g. a List<Car> or an ObservableCollection<Car> if the collection is changed at runtime (otherwise added or removed items will not be updated in the user interface). Then use an ItemsControl or ListBox or ListView depending on your requirements and add an ItemsTemplate.
public ObservableCollection<Car> Cars { get; }
<ItemsControl ItemsSource="{Binding Cars}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Brand}"/>
<TextBlock Text="{Binding Axes}"/>
<!-- ...other markup. -->
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
uj5u.com熱心網友回復:
C# 代碼
Car car = new();
itemControl.ItemsSource = car.GetType().GetProperties();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313970.html
