在XAML中定义的WPF窗口上使用泛型参数,可以通过以下步骤实现:
public class GenericWindow<T> : Window where T : class
{
public T DataContext { get; set; }
public GenericWindow()
{
InitializeComponent();
}
}
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespace">
<Style TargetType="{x:Type local:GenericWindow}">
<Setter Property="Title" Value="Generic Window" />
<Setter Property="Width" Value="800" />
<Setter Property="Height" Value="600" />
<Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}, Path=DataContext}" />
<Setter Property="Content">
<Setter.Value>
<Grid>
<!-- 在这里定义窗口的内容 -->
</Grid>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
var window = new GenericWindow<YourDataContextType>();
window.DataContext = new YourDataContextType();
window.ShowDialog();
<TextBlock Text="{Binding YourProperty}" />
通过以上步骤,您可以在XAML中定义的WPF窗口上使用泛型参数。
领取专属 10元无门槛券
手把手带您无忧上云