Reputation: 301
I am new to WPF and this is my first post. I have created a class called 'Fruit' that descends from 'DependencyObject' and adds and extra property called 'Apple'.
I have created a new custom control that includes a Dependency Property called 'MyFruit' of type 'Fruit'.
How can I set the default value for the properties within 'MyFruit' object (i.e. the 'Apple' property? I would like to set this in XAML using the <Setter> object.
public class Gauge : Control
{
.
.
.
//---------------------------------------------------------------------
#region MyFruit Dependency Property
public Fruit MyFruit
{
get { return (Fruit)GetValue(MyFruitProperty); }
set { SetValue(MyFruitProperty, value); }
}
public static readonly DependencyProperty MyFruitProperty =
DependencyProperty.Register("MyFruit", typeof(Fruit), typeof(CircularGauge), null);
#endregion
}
//-------------------------------------------------------------------------
#region Fruit class
public class Fruit : DependencyObject
{
private int apple;
public int Apple
{
get { return apple; }
set { apple = value; }
}
}
#endregion
Upvotes: 30
Views: 28393
Reputation: 1
public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(Brush), typeof(YinaKassaProductButtonCustomControl), new PropertyMetadata(null));
The default value needs to be immutable, so you can't set it to the value of another dependency property.
I myself ended up adding a property to my class like this
public Brush BorderColor => IsSelectedBorderColor ?? BackgroundColor;
Then I bind to the BorderColor in my template of my custom control. Basicly it checks if the dependency property IsSelectedBorderColor is not null and uses its value, else it uses the value of the BackgroundColor dependency property.
I use it in my triggers...
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="BorderBrush" Value="{Binding BorderColor, RelativeSource={RelativeSource Self}}"></Setter>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="{Binding BackgroundColor, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="Foreground" Value="{Binding ForegroundColor, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="BorderBrush" Value="{Binding BackgroundColor, RelativeSource={RelativeSource Self}}"></Setter>
</Trigger>
</ControlTemplate.Triggers>
Upvotes: 0
Reputation: 1517
Instead of null in your dependency property metadata, insert
new UIPropertyMetadata("YOUR DEFAULT VALUE GOES HERE")
So now it becomes:
public static readonly DependencyProperty MyFruitProperty =
DependencyProperty.Register("MyFruit", typeof(Fruit), typeof(CircularGauge), new UIPropertyMetadata("YOUR DEFAULT VALUE GOES HERE"));
Upvotes: 47
Reputation: 125
You need to use PropertyMetaData
like this:
class MyValidation
{
public bool status
{
get { return (bool)GetValue(statusProperty); }
set { SetValue(statusProperty, value); }
}
public static readonly DependencyProperty statusProperty = DependencyProperty.Register("status", typeof(bool), typeof(MyValidation),new PropertyMetadata(false));
}
Upvotes: 8