从 TextBlock 样式设置多边形可见性

Setting a Polygon Visibility from TextBlock style

我遇到了一个小问题,非常感谢任何人的建议。

问题: 我在这个道具的 RowData.Row.ScannedQty 中得到了我的价值。我将它发送到此 WarehouseInventoryDefineValueConvertor 并根据我添加的 + 或 - 检查该值是零还是加或减,但问题是设置多边形的可见性我不能像这样使用作为Dp 不可绑定我该如何实现?

我考虑的解决方案:

1.Using 代理 TextBlock 将值保存在那里并在 DataTrigger 中使用它

2.calling 我在每个多边形上的转换器(这是一个我想避免的选项,因为对转换器的调用增加了 4 倍)

 <StackPanel>
        <TextBlock Grid.Row="0" Name="TxtScannedQty" Text="{Binding RowData.Row.ScannedQty,Converter={UIConverter:WarehouseInventoryDefineValueConvertor}}" HorizontalAlignment="Right">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="{Binding ElementName=MinusPolygon,Path=Visibility}" Value="Collapsed"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=TxtScannedQty,Path=Text}" Value="Zero">
                            <Setter Property="Text" Value="{Binding RowData.Row.ScannedQty, StringFormat= {}{0}}"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=TxtScannedQty,Path=Text}" Value="Plus">
                            <Setter Property="Text" Value="{Binding RowData.Row.ScannedQty, StringFormat= +{}{0}}"></Setter>
                            <Setter Property="{Binding ElementName=MinusPolygon,Path=Visibility}" Value="Visble"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=TxtScannedQty,Path=Text}" Value="Minus">
                            <Setter Property="Text" Value="{Binding RowData.Row.ScannedQty, StringFormat= -{}{0}}"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <Grid>
            <Polygon Name="PlusPolygon"   Points="5,0 10,10, 0,10" Stroke="Green" Fill="Green" />
            <Polygon Name="MinusPolygon" Points="0,0 5,10, 10,0" Stroke="Red" Fill="Red" />
            <Line Name="Zero1" Stroke="Gray" X1="0" Y1="2" X2="10" Y2="2" SnapsToDevicePixels="True" RenderOptions.EdgeMode="Aliased"   StrokeThickness="2" />
            <Line Name="Zero2" Stroke="Gray" X1="0" Y1="4" X2="10" Y2="4" SnapsToDevicePixels="True" RenderOptions.EdgeMode="Aliased"   StrokeThickness="2" />
        </Grid>
    </StackPanel>

基于回答

我最终调用了转换器 3 次,有什么更好的方法吗?

<Style.Triggers>
                <DataTrigger Binding="{Binding RowData.Row.ScannedQty,Converter={UIConverter:WarehouseInventoryDefineValueConvertor}Value="Zero">
                    <Setter Property="Template" Value="{StaticResource ZeroTemplate}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding RowData.Row.ScannedQty,Converter={UIConverter:WarehouseInventoryDefineValueConvertor}}" Value="Plus">
                    <Setter Property="Template" Value="{StaticResource PositiveTemplate}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding RowData.Row.ScannedQty,Converter={UIConverter:WarehouseInventoryDefineValueConvertor}}" Value="Minus">
                    <Setter Property="Template" Value="{StaticResource NegativeTemplate}" />
                </DataTrigger>
            </Style.Triggers>

有很多不同的方法可以解决这个问题,我通常通过模板化现有控件来实现,例如像这样:

<Control>
    <Control.Style>
        <Style TargetType="Control">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SomeBoundProperty}" Value="Zero">
                    <Setter Property="Template" Value="{StaticResource ZeroTemplate}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding SomeBoundProperty}" Value="Plus">
                    <Setter Property="Template" Value="{StaticResource PositiveTemplate}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding SomeBoundProperty}" Value="Minus">
                    <Setter Property="Template" Value="{StaticResource NegativeTemplate}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Control.Style>
</Control>

然后您可以根据需要在资源块中声明每个模板:

<ControlTemplate x:Key="ZeroTemplate" TargetType="Control">
    <Grid>
        <!-- etc -->
    </Grid>
</ControlTemplate>

这样,可视化树中的唯一元素就是实际可见的元素。

我在这里使用了通用控件,但您当然也可以使用 TextBlock,然后将 ContentPresenter 或其他您希望实际文本显示在子模板中的位置。

如果您不想创建自定义控件或 UserControl,您可以将 TextBlockTag 属性 设置为 Visibility 值,然后将 PolygonVisibility 属性 绑定到 TextBlock:

Tag 属性
<StackPanel>
    <TextBlock Grid.Row="0" Name="TxtScannedQty" Text="{Binding RowData.Row.ScannedQty, Converter={UIConverter:WarehouseInventoryDefineValueConvertor}}" HorizontalAlignment="Right">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Tag" Value="Collapsed" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=TxtScannedQty,Path=Text}" Value="Zero">
                        <Setter Property="Text" Value="{Binding RowData.Row.ScannedQty, StringFormat= {}{0}}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=TxtScannedQty,Path=Text}" Value="Plus">
                        <Setter Property="Text" Value="{Binding RowData.Row.ScannedQty, StringFormat= +{}{0}}"></Setter>
                        <Setter Property="Tag" Value="Visible"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=TxtScannedQty,Path=Text}" Value="Minus">
                        <Setter Property="Text" Value="{Binding RowData.Row.ScannedQty, StringFormat= -{}{0}}"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    <Grid>
        <Polygon Name="PlusPolygon" Points="5,0 10,10, 0,10" Stroke="Green" Fill="Green">
            <Polygon.Style>
                <Style TargetType="Polygon">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Visibility, ElementName=MinusPolygon}" Value="Visible">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Polygon.Style>
        </Polygon>
        <Polygon Name="MinusPolygon" Points="0,0 5,10, 10,0" Stroke="Red" Fill="Red"
                         Visibility="{Binding Tag, ElementName=TxtScannedQty}" />
        <Line Name="Zero1" Stroke="Gray" X1="0" Y1="2" X2="10" Y2="2" SnapsToDevicePixels="True" RenderOptions.EdgeMode="Aliased"   StrokeThickness="2" />
        <Line Name="Zero2" Stroke="Gray" X1="0" Y1="4" X2="10" Y2="4" SnapsToDevicePixels="True" RenderOptions.EdgeMode="Aliased"   StrokeThickness="2" />
    </Grid>
</StackPanel> 

如果您不想 "call the converter 3 times",您应该 return "Zero"、"Plus" 或 "Minus" 来自来源 属性在视图模型中而不是在视图中转换值。