我最近将我的项目从 .NET 4.8.1 迁移到 .NET 8。在我的项目中,我在
Themes.Generic.xaml
ResourceDictionary 中实现了一个自定义控件,并且包含 DependencyProperties 的 C# 实现位于 MyProject.CustomControls
命名空间中。
迁移后,我的自定义控件在 XAML 中不可见。它们显示为宽度为零的空矩形。如果我手动扩展宽度,我会得到空矩形。
这是我的
Generic.xaml
:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:materialdesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:FloatingLabelComboBox">
<Style TargetType="{x:Type local:CustomControls.FloatingLabelComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControls.FloatingLabelComboBox}">
<StackPanel FlowDirection="LeftToRight">
<Label Content="{TemplateBinding LabelText}"
Foreground="{TemplateBinding LabelForeground}"
FontSize="{TemplateBinding LabelFontSize}"
VerticalAlignment="Stretch"
Margin="-5 0 0 0"
/>
<ComboBox x:Name="comboBox"
ItemsSource="{TemplateBinding ItemsSource}"
SelectedItem="{TemplateBinding SelectedItem}"
SelectedValuePath="{TemplateBinding SelectedValuePath}"
SelectedValue="{TemplateBinding SelectedValue}"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Width="Auto"
Height="Auto"
materialdesign:HintAssist.IsFloating="False"
materialdesign:ColorZoneAssist.Mode="Dark"
materialdesign:HintAssist.HintOpacity="0.10"
materialdesign:HintAssist.FontFamily="Century Gothic"
Foreground="{TemplateBinding Foreground}">
</ComboBox>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这是包含 DependencyProperties 的 C# 实现:
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace FloatingLabelComboBox.CustomControls
{
public class FloatingLabelComboBox : Control
{
static FloatingLabelComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FloatingLabelComboBox), new FrameworkPropertyMetadata(typeof(FloatingLabelComboBox)));
}
public static readonly DependencyProperty LabelTextProperty =
DependencyProperty.Register("LabelText", typeof(string), typeof(FloatingLabelComboBox), new PropertyMetadata("LabelText"));
public string LabelText
{
get { return (string)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
public static readonly DependencyProperty LabelForegroundProperty =
DependencyProperty.Register("LabelForeground", typeof(Brush), typeof(FloatingLabelComboBox), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD6931B"))));
public Brush LabelForeground
{
get { return (Brush)GetValue(LabelForegroundProperty); }
set { SetValue(LabelForegroundProperty, value); }
}
public static readonly DependencyProperty LabelFontSizeProperty =
DependencyProperty.Register("LabelFontSize", typeof(double), typeof(FloatingLabelComboBox), new PropertyMetadata(10.0));
public double LabelFontSize
{
get { return (double)GetValue(LabelFontSizeProperty); }
set { SetValue(LabelFontSizeProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(FloatingLabelComboBox), new PropertyMetadata(null));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(FloatingLabelComboBox), new PropertyMetadata(null));
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public static readonly DependencyProperty SelectedValuePathProperty =
DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(FloatingLabelComboBox), new PropertyMetadata(null));
public string SelectedValuePath
{
get { return (string)GetValue(SelectedValuePathProperty); }
set { SetValue(SelectedValuePathProperty, value); }
}
public static readonly DependencyProperty SelectedValueProperty =
DependencyProperty.Register("SelectedValue", typeof(object), typeof(FloatingLabelComboBox), new PropertyMetadata(null));
public object SelectedValue
{
get { return GetValue(SelectedValueProperty); }
set { SetValue(SelectedValueProperty, value); }
}
public static readonly DependencyProperty DisplayMemberPathProperty =
DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(FloatingLabelComboBox), new PropertyMetadata(null));
public string DisplayMemberPath
{
get { return (string)GetValue(DisplayMemberPathProperty); }
set { SetValue(DisplayMemberPathProperty, value); }
}
}
}
我已检查以下内容:
XAML 文件的构建操作是“Page”,对于 C# 文件来说,构建操作是“C# 编译器”。在 .NET 4.8.1 中,构建操作是“编译”。
这是完整项目的 github 存储库,任何帮助将不胜感激。
您应该将
ThemeInfo
属性添加到您的 .NET 8 程序集中:
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly
)]
哦!,我在 Generic.xaml 中发现了问题
x:Key="MyCustomControl"
。删除后一切正常。