Я только что запустил WPF сегодня утром, так что (надеюсь) этот вопрос легко решить.Я начал с создания кнопки с градиентным фоном.Я хочу объявить начальный и конечный цвета градиента в свойстве элемента управления, а затем применить их в шаблоне.У меня проблемы с получением кода для компиляции.Исключение, которое я получаю, заключается в том, что xaml сообщает мне, что свойство недоступно, но когда я переключаю модификатор видимости на public, он жалуется, что не может найти статическое свойство ...
Вот мой xamlдо сих пор:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="my:GradientButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type my:GradientButton}">
<Grid>
<Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Top" HorizontalAlignment="Left">
<Ellipse.Fill>
<LinearGradientBrush>
<GradientStop Color="{TemplateBinding GradientStart}" Offset="0"></GradientStop><!--Problem on this line!!!-->
<GradientStop Color="{TemplateBinding GradientEnd}" Offset="1"></GradientStop>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Polygon Points="18,12 18,38, 35,25" Fill="{TemplateBinding Foreground}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
<my:GradientButton x:Name="btnPlay" Height="50" Width="50" Foreground="Black" Click="Button_Click" GradientStart="#CCCCCC" GradientEnd="#7777777" />
</StackPanel>
А вот код для моего пользовательского элемента управления:
public class GradientButton : Button
{
static DependencyProperty GradientStartProperty;
static DependencyProperty GradientEndProperty;
static GradientButton()
{
GradientStartProperty = DependencyProperty.Register("GradientStart", typeof(Color), typeof(GradientButton));
GradientEndProperty = DependencyProperty.Register("GradientEnd", typeof(Color), typeof(GradientButton));
}
public Color GradientStart
{
get { return (Color)base.GetValue(GradientStartProperty); }
set { base.SetValue(GradientStartProperty, value); }
}
public Color GradientEnd
{
get { return (Color)base.GetValue(GradientEndProperty); }
set { base.SetValue(GradientEndProperty, value); }
}
}
РЕДАКТИРОВАТЬ: Вот исключение во время разработки я получаю
Cannot reference the static member 'GradientStartProperty' on the type 'GradientButton' as it is not accessible.