我背後有這樣的代碼:WPF用戶控件與通用的代碼隱藏
CustomUserControl.xaml.cs
namespace MyProject
{
public partial class CustomUserControl<T> : UserControl
{
...
}
}
與此XAML:
CustomUserControl.xaml
<UserControl x:Class="MyProject.CustomUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Grid>
</Grid>
它不起作用,因爲x:Class =「MyProject.CustomUserControl」與代碼隱藏的泛型類定義不匹配。有什麼方法可以使這項工作?
我有種看到你在做什麼,但我不是在XAML結構是最好的。 您如何/在哪裏指定控件的外觀?我已將所有代碼複製到測試項目中,但我無法:定義它應該是什麼樣子,如何在MainWindow.xaml中使用此「UserControl」,以及如何將數據綁定到它,例如,將'MyGeneric(Of T)'綁定到'Label'的'Content'。 – 2017-05-08 14:07:29
@Zach我爲你寫了一個小例子。看看[this](https://github.com/timmi-on-rails/GenericUserControlWPF)。 – Tom 2017-05-09 20:54:58