C# 我的参数创建不起作用usercontrol wpf }

C# 我的参数创建不起作用usercontrol wpf },c#,xaml,c#-4.0,C#,Xaml,C# 4.0,请尝试下面的代码,它对我有用。为了简单起见,我刚刚包含了ControlWidth的代码 <us:UCDataGrid x:Name="d" ControlWidth="1" ControlHeight="10" /> 现在在xaml中为UserControl设置名称并访问它以设置绑定。暂时删除所有样式,并检查以确保没有在任何其他位置设置宽度和高度 public partial class UCDataGrid : UserControl { public UCDat

请尝试下面的代码,它对我有用。为了简单起见,我刚刚包含了ControlWidth的代码

    <us:UCDataGrid x:Name="d" ControlWidth="1" ControlHeight="10" />
现在在xaml中为UserControl设置名称并访问它以设置绑定。暂时删除所有样式,并检查以确保没有在任何其他位置设置宽度和高度

public partial class UCDataGrid : UserControl
{
    public UCDataGrid()
    {
        InitializeComponent();
    }

    public static  DependencyProperty ControlWidthProperty =
    DependencyProperty.Register("ControlWidth", typeof(double), typeof(UCDataGrid),new UIPropertyMetadata());

    public double ControlWidth
    {
       get { return (double)GetValue(ControlWidthProperty); }
       set { SetValue(ControlWidthProperty, value);  }
    }
}


希望能有所帮助

您遇到了什么错误?你能分享一下UCDataGrid的代码吗?]@JayakrishnanGounder你能帮我吗当我在项目测试中调用datagrid时,他告诉我值无效哪个值?你能分享更多细节吗?当我在我的Xaml中调用ControlWidth并在这里给出值时,我发现我的值中有错误15是的,它可以工作,只是问题转换为int
    <us:UCDataGrid x:Name="d" ControlWidth="1" ControlHeight="10" />
public partial class UCDataGrid : UserControl
{
    public UCDataGrid()
    {
        InitializeComponent();
    }

    public static  DependencyProperty ControlWidthProperty =
    DependencyProperty.Register("ControlWidth", typeof(double), typeof(UCDataGrid),new UIPropertyMetadata());

    public double ControlWidth
    {
       get { return (double)GetValue(ControlWidthProperty); }
       set { SetValue(ControlWidthProperty, value);  }
    }
}
<UserControl Name="root">
 <Border Grid.Row="0"  Height="auto" x:Name="BorderEcs" Background="#9494a5"   BorderThickness="1,0,1,1" BorderBrush="#9494a5"  CornerRadius="10,10,0,0">
    <StackPanel Height="auto">
        <Label x:Name="LblTitl" Content="" Margin="5,0,0,0" Height="auto"  Foreground="#FFFFFF" FontFamily="Century Gothic" FontSize="13" />
        <DataGrid  Width="{Binding ElementName=root, Path=ControlWidth, UpdateSourceTrigger=PropertyChanged}" 
                   Height="{Binding ElementName=root, Path=ControlHeight, UpdateSourceTrigger=PropertyChanged}"           
                       CellStyle="{StaticResource DataGridContentCellCentering}"
                       RowStyle="{StaticResource RowStyle}" 
                       ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}" 
                       Style="{StaticResource DataGridStyle}"  
                       AlternatingRowBackground="White" 
                       AlternationCount="2">
        </DataGrid>
    </StackPanel>
    </UserControl>