我正在尝试让设计数据显示在我的设计器预览中。当我运行程序时,UserControl 工作正常;但是我无法让设计师填充值。
首先,我的 UserControl 名为 SignalStrengthControl
<UserControl x:Class="Connection.SignalStrengthControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Connection"
mc:Ignorable="d"
d:DesignHeight="53.833" d:DesignWidth="100.917"
d:DataContext="{d:DesignData Source=DesignData/SignalStrengthControlDesignData.xaml}">
<Grid Background="Transparent">
<Grid.Resources>
<SolidColorBrush x:Key="OnColor" Color="#FF4a6bc8"></SolidColorBrush>
<SolidColorBrush x:Key="OffColor" Color="#50FFFFFF"></SolidColorBrush>
<local:RatingConverter x:Key="RatingConverter" OnBrush="{StaticResource OnColor}" OffBrush="{StaticResource OffColor}" />
<Style TargetType="Rectangle">
<Setter Property="VerticalAlignment" Value="Bottom" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width=".4*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width=".4*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width=".4*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width=".4*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="4*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="1"
Fill="{Binding Path=RatingValue, ConverterParameter=1, Converter={StaticResource RatingConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SignalStrengthControl}}}"
Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"/>
</Grid>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="3*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="1"
Fill="{Binding Path=RatingValue, ConverterParameter=2, Converter={StaticResource RatingConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SignalStrengthControl}}}"
Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"/>
</Grid>
<Grid Grid.Column="4">
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="3*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="1"
Fill="{Binding Path=RatingValue, ConverterParameter=3, Converter={StaticResource RatingConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SignalStrengthControl}}}"
Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"/>
</Grid>
<Grid Grid.Column="6">
<Grid.RowDefinitions>
<RowDefinition Height="1*"></RowDefinition>
<RowDefinition Height="4*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="1"
Fill="{Binding Path=RatingValue, ConverterParameter=4, Converter={StaticResource RatingConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SignalStrengthControl}}}"
Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"/>
</Grid>
<Grid Grid.Column="8">
<Grid.RowDefinitions>
<RowDefinition Height="0*"></RowDefinition>
<RowDefinition Height="5*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="1"
Fill="{Binding Path=RatingValue, ConverterParameter=5, Converter={StaticResource RatingConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SignalStrengthControl}}}"
Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"/>
</Grid>
</Grid>
</UserControl>
您可以看到我有一个可忽略的 DataContext 指向一些设计数据(查找文件没有问题)。这个 UserControl 背后的想法是它包含一个枚举依赖属性,并且它有一个枚举转换器来控制条形的填充,从而创建类似于 wifi 信号强度指示器的东西。
<local:SignalStrengthControlDesignData
xmlns:local="clr-namespace:Connection.DesignData"
xmlns:connection="clr-namespace:Connection"
xmlns:markup="http://schemas.microsoft.com/winfx/2006/xaml"
RatingValue="FourBars"
/>
<!--RatingValue="{markup:Static connection:SignalStrength.FourBars}"-->
这是我的 SignalStrengthControlDesignData.xaml。我尝试了注释掉的代码,它也不起作用。我相信“FourBars”被解释为一个枚举,因为自动完成说它是一个枚举成员。下面是后面的设计数据代码。
internal sealed class SignalStrengthControlDesignData
{
public SignalStrength RatingValue { get; set; }
}
这是 SignalStrengthControl 背后的代码,具有依赖属性、枚举和转换器。
public partial class SignalStrengthControl
{
public SignalStrengthControl()
{
InitializeComponent();
}
public SignalStrength RatingValue
{
get { return (SignalStrength)GetValue(RatingValueProperty); }
set { SetValue(RatingValueProperty, value); }
}
// Using a DependencyProperty as the backing store for RatingValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty RatingValueProperty =
DependencyProperty.Register("RatingValue", typeof(SignalStrength), typeof(SignalStrengthControl), new UIPropertyMetadata(SignalStrength.ZeroBars));
}
public enum SignalStrength
{
ZeroBars,
OneBar,
TwoBars,
ThreeBars,
FourBars,
FiveBars
}
public class RatingConverter : IValueConverter
{
public Brush OnBrush { get; set; }
public Brush OffBrush { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
{
return Brushes.Transparent;
}
// There are five bars in the xaml with each having a barNumber associated with it. The smallest bar
// on the left has a barNumber value of 1, the largest has 5.
int barNumber;
if (int.TryParse(parameter.ToString(), out barNumber))
{
var signalStrengthRating = (int)value;
if (barNumber <= signalStrengthRating)
{
return this.OnBrush;
}
return this.OffBrush;
}
return Brushes.Transparent;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我也用 Brushes.WhateverColor 替换了 this.OnBrush,它仍然没有出现在设计器中。
我知道显示某些东西的一种方法是拥有一个 FallBackValue,但将来我可能需要比这个显示更复杂的东西。
接受所有建议。