在 WPF DataTemplate 中对颜色进行数据绑定

发布于 2024-12-09 01:02:11 字数 1644 浏览 0 评论 0原文

这个问题已经困扰我一段时间了,我似乎无法让它发挥作用。我正在使用数据模板来格式化 ListBox 控件中的项目,如下所示:

<ListBox HorizontalAlignment="Stretch" Name="listBox1" VerticalAlignment="Stretch" Grid.ColumnSpan="3" Grid.Row="1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="100*" />
                </Grid.ColumnDefinitions>
                <Rectangle Width="30" Height="10" Grid.Column="0" 
                    VerticalAlignment="Stretch" 
                    HorizontalAlignment="Stretch" 
                    Fill="{Binding Path=Color, 
                        Converter={StaticResource SolidColorBrushConverter}, 
                        Mode=OneWay}" />
                <CheckBox Grid.Column="1" 
                    VerticalAlignment="Center" 
                    HorizontalAlignment="Center" 
                    IsChecked="{Binding Path=Selected}" />
                <Label Grid.Column="2" VerticalAlignment="Center" 
                    Content="{Binding Path=Name}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

问题是矩形未按应有的方式显示绑定颜色。跟踪转换器,绑定似乎从绑定项正确获取颜色(复选框和标签上的其他绑定按预期工作)。另外,我在调试输出中没有收到任何绑定错误。

我还尝试将矩形包装在带有黑色边框的 元素中,该元素显示正确,所以我知道矩形至少显示出来,但是替换 BorderBrush 属性与矩形的 Fill 属性具有相同的绑定(如上所示)会导致边框消失(同样,没有绑定错误)。

如何在 WPF 中正确实现颜色的绑定?

This one has been bugging me for a while and I cant seem to get it to work. I am using a Data Template to format items within a ListBox control, like so:

<ListBox HorizontalAlignment="Stretch" Name="listBox1" VerticalAlignment="Stretch" Grid.ColumnSpan="3" Grid.Row="1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="100*" />
                </Grid.ColumnDefinitions>
                <Rectangle Width="30" Height="10" Grid.Column="0" 
                    VerticalAlignment="Stretch" 
                    HorizontalAlignment="Stretch" 
                    Fill="{Binding Path=Color, 
                        Converter={StaticResource SolidColorBrushConverter}, 
                        Mode=OneWay}" />
                <CheckBox Grid.Column="1" 
                    VerticalAlignment="Center" 
                    HorizontalAlignment="Center" 
                    IsChecked="{Binding Path=Selected}" />
                <Label Grid.Column="2" VerticalAlignment="Center" 
                    Content="{Binding Path=Name}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The problem is that the rectangle does not appear with the bound color, as it should. Tracing the converter, the binding seems to get the color correctly from the bound item (the other bindings on the checkbox and the label work as expected). Also I get no binding errors in the debug output.

I also tried wrapping the rectangle in a <border> element with a black border, which shows correctly, so I know the rectangle is at least showing up, however replacing the BorderBrush attribute with the same binding from the rectangle's Fill attribute as shown above causes the border to disappear (again, with no binding errors).

How does one correctly achieve binding of a Color in WPF?

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

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

发布评论

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

评论(1

弥繁 2024-12-16 01:02:11

该代码没有问题,故障一定出在转换器上。也许它获取了颜色,但将 alpha 通道设置为 0,使所有颜色完全透明。如果不是这样,您应该发布转换器的代码,以便人们可以查看它。

您也可以轻松地在没有任何转换器的情况下完成:

<Rectangle.Fill>
    <SolidColorBrush Color="{Binding Color}"/>
</Rectangle.Fill>

Nothing wrong with that code, the fault has to be with the converter. Maybe it gets the color but sets the alpha channel to 0, making all colors fully transparent. If that is not it you should post the code of the converter so people can have a look at it.

Also you can easily get by without any converters:

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