Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 继承和限制_C#_.net_Wpf_Vb.net_Wpf Controls - Fatal编程技术网

C# 继承和限制

C# 继承和限制,c#,.net,wpf,vb.net,wpf-controls,C#,.net,Wpf,Vb.net,Wpf Controls,我需要一个控件,比如WPF中的标签。但此标签应始终自动调整大小 (宽度=高度=自动)-用户不能修改它 此外,边界Thinkness应始终为=0 我怎样才能在WPF中做到这一点? 是否可以继承这样的标签,以便子级保留该属性(始终自动调整大小)?制作一个包装标签的用户控件,并进行硬编码 Width=Height=Auto 和 BorderThickness=0 比如: <UserControl ...> <Label Width="Auto" Height="Auto" Bo

我需要一个控件,比如WPF中的标签。但此标签应始终自动调整大小
(宽度=高度=自动)-用户不能修改它

此外,边界Thinkness应始终为=0

我怎样才能在WPF中做到这一点?
是否可以继承这样的标签,以便子级保留该属性(始终自动调整大小)?

制作一个包装标签的用户控件,并进行硬编码
Width=Height=Auto


BorderThickness=0

比如:

<UserControl ...>
   <Label Width="Auto" Height="Auto" BorderThickness="0" />
</UserControl>

我只是编造了它,所以它可能需要一些调试,但你明白了;)

宽度和高度设置器最终都通过调用虚拟SetBoundsCore方法应用边界修改。覆盖此方法可能是您建议修改的首选候选方法。

您是否考虑过拥有一个应用程序范围的资源字典并定义样式?定义一次,您就可以在任何需要的地方使用

在AppResources.xaml中:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    >
    <Style x:Key="AutoSizeLabel" TargetType="{x:Type Label}">
        <Setter Property="Width" Value="Auto" />
        <Setter Property="Height" Value="Auto" />
        <Setter Property="BorderThickness" Value="0" />
    </Style>
</ResourceDictionary>


在App.xaml中

    <Application.Resources>
        <ResourceDictionary Source="AppResources.xaml" />
    </Application.Resources>


在MainWindow.xaml中

<Grid>
    <Label Style="{StaticResource ResourceKey=AutoSizeLabel}">MyLabel</Label>
</Grid>

迈拉贝尔

据我所知,您可以创建一个“继承自”标签的CustomControl。我还没有在WPF中构建CustomControls(但是在ASP.NET中,您可以这样做)。否则可能会创建自己的控件:@Nicholas问题是如何创建,问题是如何实现宽度和高度的非修改。是否应该通过重载ind或隐藏Wifdth、Height、BorderThickness属性?…如果仍然希望以只读模式访问底层基类的值,我的第一个猜测是将它们设置为只读。如果您尝试在xaml中设置它们,将生成一个编译器错误。所以你必须要对那些财产进行跟踪是的。。。我必须通过依赖属性公开所有其他属性。。。但可能只是在继承标签的新CustomControl中重载宽度和高度属性?不幸的是,您不能重载这些属性注意:重载,而不是重写!)我认为您将重载误认为重写,因为您也不能“重载”属性(好吧),这将适用于该类,但是。。。问题的题目是“继承和限制”,所以我恐怕不能继承一种风格。
<Grid>
    <Label Style="{StaticResource ResourceKey=AutoSizeLabel}">MyLabel</Label>
</Grid>