【问题标题】:How can I deal with the repeat without the IValueConverter?如果没有 IValueConverter,我该如何处理重复?
【发布时间】:2019-08-22 03:33:41
【问题描述】:

我用 WPF 编写了一个自定义控件,我想这样做:

1.按百分比设置填充

2.自动将top/left/right/bottom(Padding)全部设置为top/left/right/bottom(Padding)的最小值。


这是Generic.xaml中的代码

  <Style TargetType="{x:Type local:SC}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SC}">
                    <Grid Margin="{TemplateBinding Margin}" Background="{TemplateBinding Background}" >
                        <Grid Margin="{TemplateBinding Padding}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Bind something"></ColumnDefinition>
                                <ColumnDefinition Width="Bind something"></ColumnDefinition>
                                <ColumnDefinition Width="Bind something"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Bind something"></RowDefinition>
                                <RowDefinition Height="Bind something"></RowDefinition>
                                <RowDefinition Height="Bind something"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid Grid.Column="1" Grid.Row="1">
                                <ContentPresenter Content="{TemplateBinding Content}"></ContentPresenter>
                            </Grid>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我的想法是使用 3 行和 3 列,分别设置 GridLength 并将内容设置为 Grid.Column="1"&Grid.Row="1" 来做到这一点。

但是在我写了一些代码之后,我发现我必须使用一个multibinding&Converter , 多次计算 GridLength 来做。我认为结果是一样的,所以重复计算是没有用的。更重要的是,如果项目中有很多这个控件,它会降低性能。

恐怕我还没有解释清楚,造成一些误解。但我想学习如何实现这一目标。谢谢。

【问题讨论】:

  • 您是否尝试过改用共享大小的列/行?
  • 您是否担心浪费性能只是因为您不喜欢 CPU 多次执行相同的计算,即使它可能在几微秒内完成,或者您有吗?屏幕更新频率/帧率的视觉问题?
  • 你怎么能有 3 行,每行都是他们父母身高的一半? 3*=0.5 = 可用空间的 150%....
  • 不,有些不同,我希望列和行的长度与 ActualHeight/2 相同,但不只是共享大小。@H.B.
  • @MartinLiversage 首先,也许正如您所说,只需几微秒即可完成。但是我在项目中使用的这个自定义控件越多,花费的时间就越多。更重要的是,我认为我不应该这样编码。

标签: wpf


【解决方案1】:

在这种情况下,列或行中的星号属性将使所有大小相等,这将导致 2 列是大小的一半。

您还可以将一排设置为另一排的 2 倍大,其中一排为 "*",第二排 2 以 "2*" 开头。

在你的情况下替换这个:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="{TemplateBinding ActualHeight,Converter={StaticResource GridLengthConverter}}"></ColumnDefinition>
    <ColumnDefinition Width="{TemplateBinding ActualHeight,Converter={StaticResource GridLengthConverter}}"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="{TemplateBinding ActualHeight,Converter={StaticResource GridLengthConverter}}"></RowDefinition>
    <RowDefinition Height="{TemplateBinding ActualHeight,Converter={StaticResource GridLengthConverter}}"></RowDefinition>
</Grid.RowDefinitions>

与:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"></ColumnDefinition>
    <ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="*"></RowDefinition>
    <RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>

编辑:

<Grid.RowDefinitions>
    <RowDefinition Height="4*"></RowDefinition>
    <!-- This will take 80 % of the space available-->
    <RowDefinition Height="*"></RowDefinition>
    <!-- This will take 20 % of the space available-->
</Grid.RowDefinitions>

【讨论】:

  • 好的,非常感谢。但是我很好奇一件事:如果计算更复杂(例如(ActualHeight+ActualWidth)/2)那么就不能使用星号。我怎么解决这个问题。谢谢。
  • 当你想使用多重属性时,例如将控件宽度与高度相关联,你将需要一个转换器,但对于基本的东西,如大小相同或特定比例,你可以使用星号
  • 好的,谢谢。看来我没有选择做复杂的。
  • 您能否更新您的代码以显示您的具体情况?也许有更好的方法我们可以一起解决
  • 我编辑了这个主题并更详细地介绍了它。造成一些误解,我深表歉意。
猜你喜欢
  • 2016-08-14
  • 2016-09-22
  • 2021-07-29
  • 1970-01-01
  • 2021-03-03
  • 2011-04-17
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
相关资源
最近更新 更多