RadBarCodeQr - 根据图像大小保持与图像相同的距离

发布于 2025-01-16 07:03:17 字数 1900 浏览 2 评论 0 原文

我可以控制 RadBarCodeQR,并且我希望它与绑定到它的任何文本与其父级保持相等的间距。

RadBarCodeQR 绑定到空字符串时:

RadBarCodeQR 绑定到“ssssssssssssssssss”时:

包含“ssssssssssssssssss”的文本框,其下方有一个条形码,周围没有空格。

我的代码:

<Grid>
    <StackPanel VerticalAlignment="Center">
        <TextBox 
            Text="{Binding QrCode, UpdateSourceTrigger=PropertyChanged}" 
            Width="300" 
            Height="30" 
            Margin="20"/>
        <telerik:RadBarcodeQR
            Height="100" 
            Width="100"
            Text="{Binding QrCode, UpdateSourceTrigger=PropertyChanged}"
            Foreground="Black"
            UseLayoutRounding="True">
            <telerik:RadBarcodeQR.Style>
                <Style TargetType="telerik:RadBarcodeQR">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="telerik:RadBarcodeQR">
                                <Grid 
                                    x:Name="PART_RadBarcodeQR" 
                                    Background="White" 
                                    Margin="15">
                                    <Image 
                                        x:Name="ImageQrCode" 
                                        HorizontalAlignment="Center" 
                                        VerticalAlignment="Center"/>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </telerik:RadBarcodeQR.Style>
        </telerik:RadBarcodeQR>
    </StackPanel>
</Grid> 

I have control of RadBarCodeQR and I want that it maintains an equal spacing from its parent for any text bound to it.

When RadBarCodeQR is bound to an empty string:

Empty TextBox with a BarCode unterneath that has a white space around it.

And when RadBarCodeQRbound to "ssssssssssssssssss":

TextBox containing "ssssssssssssssssss" with a BarCode unterneath that does not have a white space around it.

My Code:

<Grid>
    <StackPanel VerticalAlignment="Center">
        <TextBox 
            Text="{Binding QrCode, UpdateSourceTrigger=PropertyChanged}" 
            Width="300" 
            Height="30" 
            Margin="20"/>
        <telerik:RadBarcodeQR
            Height="100" 
            Width="100"
            Text="{Binding QrCode, UpdateSourceTrigger=PropertyChanged}"
            Foreground="Black"
            UseLayoutRounding="True">
            <telerik:RadBarcodeQR.Style>
                <Style TargetType="telerik:RadBarcodeQR">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="telerik:RadBarcodeQR">
                                <Grid 
                                    x:Name="PART_RadBarcodeQR" 
                                    Background="White" 
                                    Margin="15">
                                    <Image 
                                        x:Name="ImageQrCode" 
                                        HorizontalAlignment="Center" 
                                        VerticalAlignment="Center"/>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </telerik:RadBarcodeQR.Style>
        </telerik:RadBarcodeQR>
    </StackPanel>
</Grid> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2025-01-23 07:03:17

我认为不可能在控件周围保持相等的间距。 QR 码有不同的大小,并具有强制边距,如 文档。这是由规范给出的,不维护它可能会导致二维码不可读。

QRCode 共有 40 个版本,从 21 x 21 模块到 177 x 177 模块,以每侧 4 个模块的步长增加。当然,更高版本用于编码更大量的数据:[...]

此外,每个二维码周围都有一个强制性的 4 个模块宽的空白区域:[...]

控件将渲染以下图像: QR 码,正如您从控件模板中看到的那样。该图像包含白色边缘,其厚度根据 QR 码尺寸而变化。 MarginPadding 不会影响这一点。话虽这么说,您对控件模板的更改 - 设置 Margin="15" - 破坏了至少绘制规范所需的边距的默认绘制行为。

因此,您应该删除自定义控件模板。布局舍入应该不是必需的,因为该控件已优化以无论如何显示 QR 代码。只有实际更改源属性的控件才需要 UpdateSourceTrigger,该控件不需要,它只读取值。

<telerik:RadBarcodeQR
   Text="{Binding QrCode}" 
   Foreground="Black"
   Background="White"
   Height="100"
   Width="100"/>

您似乎正在使用旧的 RadBarcodeQR ,即 已弃用。您可以使用 RadBarcode相反:

<telerik:RadBarcode Value="{Binding QrCode}"
                    Height="100" 
                    Width="100"> 
   <telerik:RadBarcode.Symbology> 
      <telerik:QRCode /> 
   </telerik:RadBarcode.Symbology> 
</telerik:RadBarcode> 

如果您想要额外的固定大小的空间,您可以为其添加一个 Padding

I do not think that it is possible to maintain an equal spacing around the control. The QR code comes in different sizes and has a mandatory margin as stated in the documentation. This is given the by specification and not maintaining it can lead to unreadable QR codes.

There are a total of 40 versions available in the QRCode, from 21 by 21 modules to 177 by 177 modules, increasing in steps of 4 modules per side. Naturally, higher versions are used to encode larger amounts of data: [...]

Additionally, around each QR code, there is an obligatory 4-modules-wide white space area: [...]

The control will render an image of the QR code, as you can see from the control template. This image contains a white margin varying in thickness depending on the QR code size. The Margin and Padding do not affect this. That being said, your change to the control template - setting Margin="15" - breaks the default drawing behavior that at least draws the margin required by the specification.

Consequently, you should remove your custom control template. Layout rounding should not be necessary, as the control is optimized to display a QR code anyway. The UpdateSourceTrigger is only needed for controls that actually change the source property, this control does not, it only reads the value.

<telerik:RadBarcodeQR
   Text="{Binding QrCode}" 
   Foreground="Black"
   Background="White"
   Height="100"
   Width="100"/>

You seem to be using the old RadBarcodeQR which is deprecated. You can use RadBarcode instead:

<telerik:RadBarcode Value="{Binding QrCode}"
                    Height="100" 
                    Width="100"> 
   <telerik:RadBarcode.Symbology> 
      <telerik:QRCode /> 
   </telerik:RadBarcode.Symbology> 
</telerik:RadBarcode> 

If you want to have an additional fixed-size space, you can add a Padding to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文