UserControl - DependencyProperty- MainWindow中的默认值?

时间:2014-10-21 15:07:14

标签: c# wpf xaml user-controls

我有一个UserControl,除了默认值外,在运行时完美运行。我在MainWindow中的两个不同选项卡中使用了相同的UserControl(MaterialUserControl)。在其中一个标签中,它的默认值(RadioButton isChecked值)工作正常,但另一个没有?

//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>

是否与DataContext有关? 更改出现在VS设计器中,但所有RadioButtons值都保持为空。

2 个答案:

答案 0 :(得分:1)

因为您要将依赖项属性的默认值设置为null。而不是null,将它们设置为false(或根据您的要求设置为true)

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

答案 1 :(得分:0)

我猜这是因为一个标签中的单选按钮使用与另一个标签中的单选按钮相同的GroupName,因此它们被视为同一逻辑组的一部分,即使它们分布在标签上:在一个选项卡中选择一个单选按钮,取消选择另一个选项卡的相应组中的单选按钮。尝试生成唯一的组名。

此外,null不是类型bool的属性的有效默认值;要么设置默认值false,要么将属性迁移到bool?类型。