BhupinderTube
BhupinderTube

Reputation: 51

UserControl - DependencyProperty- default values in the MainWindow?

I have a UserControl that works perfectly at run time except the default values. I have used the same UserControl (MaterialUserControl) in two different Tabs in the MainWindow. In one of the Tabs it's default values (RadioButton isChecked values) work fine but not the other?

//This is the one which is not setting its default values!
<l:MaterialUserControl x:Name="materialShellUC" stainlessSteelBool="True" Click="shellMaterialBtn_Click" plateBool="True" ENBool="True"/> 
//This one is in another Tab
<l:MaterialUserControl x:Name="materialDishUC" Width="500" HorizontalAlignment="Left" Click="materialDish_Click" plateBool="True" stainlessSteelBool="True" ENBool="True"/>

public partial class MaterialUserControl : UserControl
{
    public MaterialUserControl()
    {
        InitializeComponent();
        rootGrid.DataContext = this;
    }


    public static readonly DependencyProperty MaterialProperty =
        DependencyProperty.Register("Material", typeof(string),
        typeof(MaterialUserControl), new PropertyMetadata(""));

    public String Material
    {
        get { return (String)GetValue(MaterialProperty); }
        set { SetValue(MaterialProperty, value); }
    }

    public static readonly DependencyProperty MaterialNoProperty =
      DependencyProperty.Register("MaterialNo", typeof(string),
      typeof(MaterialUserControl), new PropertyMetadata(""));

    public String MaterialNo
    {
        get { return (String)GetValue(MaterialNoProperty); }
        set { SetValue(MaterialNoProperty, value); }
    }


    public event RoutedEventHandler Click;

    void onButtonClick(object sender, RoutedEventArgs e)
    {
        if (this.Click != null)
            this.Click(this, e);
    }

    public static readonly DependencyProperty stainlessSteelBoolProperty =
        DependencyProperty.Register("strainlessSteelBool", typeof(bool), typeof(MaterialUserControl), new PropertyMetadata(null));
    public bool stainlessSteelBool
    {
        get { return (bool)GetValue(stainlessSteelBoolProperty); }
        set { SetValue(stainlessSteelBoolProperty, value); }
    }

    public static readonly DependencyProperty plateBoolProperty =
     DependencyProperty.Register("plateBool", typeof(bool), typeof(MaterialUserControl), new PropertyMetadata(null));
    public bool plateBool
    {
        get { return (bool)GetValue(plateBoolProperty); }
        set { SetValue(plateBoolProperty, value); }
    }

    public static readonly DependencyProperty ENBoolProperty =
     DependencyProperty.Register("ENBool", typeof(bool), typeof(MaterialUserControl), new PropertyMetadata(null));
    public bool ENBool
    {
        get { return (bool)GetValue(ENBoolProperty); }
        set { SetValue(ENBoolProperty, value); }
    }


}


 //here is the UserControl XAML 
<Grid Background="Pink" x:Name="rootGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Text="Material" Grid.Row="0" Foreground="DarkBlue"/>
        <RadioButton Content="ASME" Grid.Row="1" GroupName="a"/>
        <RadioButton Content="EN" Grid.Row="1" Grid.Column="1" GroupName="a" IsChecked="{Binding ENBool}"/>
        <RadioButton Content="Plate" IsChecked="{Binding plateBool}" Grid.Row="2" GroupName="b"/>
        <RadioButton Content="Tube" Grid.Row="2" Grid.Column="1" GroupName="b"/>
        <RadioButton Content="Stainless steel" Grid.Row="3" GroupName="c" IsChecked="{Binding   stainlessSteelBool}"/>
        <RadioButton Content="Carbon Steel" Grid.Row="3" Grid.Column="1" GroupName="c"/>
        <Button Content="Material" Grid.Row="4" Click="onButtonClick"/>
        <TextBox Grid.Row="4" Grid.Column="1" IsReadOnly="True" Text="{Binding Path=Material}"/>
        <TextBox Grid.Row="4" Grid.Column="2" IsReadOnly="True" Text="{Binding     Path=MaterialNo}"/>
    </Grid>

Is it something to do with the DataContext? Changes appear in the VS designer but all the RadioButtons value stays null.

Upvotes: 0

Views: 2319

Answers (2)

Mike Strobel
Mike Strobel

Reputation: 25623

I would guess this is because the radio buttons in one tab use the same GroupName as the radio buttons in another tab, so they are considered part of the same logical group even though they are spread across tabs: when you select a radio button in one tab, you deselect a radio button in the corresponding group of another tab. Try generating unique group names.

Also, null is not a valid default value for a property of type bool; either make the default value false, or migrate your properties to type bool?.

Upvotes: 0

Krishna
Krishna

Reputation: 1996

Because you are setting the default values to the dependency properties to be null. Instead of null, set them to false (or true depending on your requirements)

public static readonly DependencyProperty plateBoolProperty =
     DependencyProperty.Register("plateBool", typeof(bool), typeof(MaterialUserControl), new PropertyMetadata(false));

Upvotes: 1

Related Questions