我正在尝试将设计数据显示在我的设计师预览中。运行程序时,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();
}
}
我也已将this.OnBrush替换为Brushes.WhatColor,它仍然没有出现在设计器中。
我知道一种显示某种东西的方法是拥有一个FallBackValue,但是将来我可能需要比显示的更为复杂的东西。
打开所有建议。
答案 0 :(得分:0)
我将实现更改为使用自定义控件,该控件将覆盖ProgressBar WPF控件。然后设计数据按预期显示。