簡體   English   中英

如果WPF中的Rect圖形為空,如何停止?

[英]How do I stop a Rect drawing in WPF if it's empty?

感覺應該很簡單,但是我無法使它正常工作。

我有一個<Rectangle>上的<Canvas> ,其中所述DataContext<Rectangle>結合於Rect ,並且我試圖用它繪制<Rectangle>屬性Canvas.Left="{Binding Left}"Width="{Binding Width}" (垂直組件類似)。

如果那里有一個矩形,這很好,但是在綁定的Rect為空的情況下(即IsEmpty == True ),這使我Value produced by BindingExpression is not valid for target property很多Value produced by BindingExpression is not valid for target propertyValue produced by BindingExpression is not valid for target property ,這些值Value produced by BindingExpression is not valid for target property具有無限值的Value produced by BindingExpression is not valid for target property錯誤Value produced by BindingExpression is not valid for target property

我已經試過設置Visibility ,以Collapsed使用的數據觸發IsEmpty屬性設置能見度倒塌,我試圖做同樣的父畫布上,但無論我做什么,我還是讓這些綁定表達式錯誤。

有什么辦法可以簡單地使它變成“如果它是空的,甚至不要嘗試繪制它”?

我想我用類似的方法解決了類似的問題:

<Rectangle Canvas.Left="{Binding Left}"
       Canvas.Top="{Binding Top}">
    <Rectangle.Style>
        <Style TargetType="{x:Type Rectangle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEmpty}" Value="True">
                    <Setter Property="Canvas.Left" Value="0"/>
                    <Setter Property="Canvas.Top" Value="0"/>
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>             
            </Style.Triggers>
        </Style> 
    </Rectangle.Style>
</Rectangle>

因此,基本上是“打破”對無效DataContext值的綁定,並在XAML中設置一個固定值。

注意:這是手寫XAML,可能包含語法錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM