簡體   English   中英

WPF ListBox中的自定義控件

[英]Custom controls in a WPF ListBox

我創建了許多控件,這些控件(稍微)擴展了許多UIElement的功能,以包括IsError和IsCorrect屬性,這些屬性用於向用戶表示錯誤情況。

例如,擴展的復選框。

public class EBC_CheckBox : CheckBox
{
    public static readonly DependencyProperty IsCorrectProperty = DependencyProperty.Register("IsCorrect", typeof(bool), typeof(EBC_CheckBox), new UIPropertyMetadata(false));
    public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register("IsError", typeof(bool), typeof(EBC_CheckBox), new UIPropertyMetadata(false));

    public EBC_CheckBox() : base()
    {
        IsError = false;
        IsCorrect = false;
    }

    public bool IsError
    {
        get { return (bool)GetValue(IsErrorProperty); }
        set { SetValue(IsErrorProperty, value); }
    }

    public bool IsCorrect
    {
        get { return (bool)GetValue(IsCorrectProperty); }
        set { SetValue(IsCorrectProperty, value); }
    }

XAML代碼中的樣式如下:

<Style TargetType="{x:Type local:EBC_CheckBox}">
    <Setter Property="Foreground" Value="{StaticResource SBase02}"/>
    <Setter Property="Background" Value="{StaticResource SBase0}"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Margin" Value="0,2,8,2"/>
    <Setter Property="BorderBrush" Value="{StaticResource SYellow}"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{StaticResource SBase01}"/>
            <Setter Property="BorderThickness" Value="0"/>
        </Trigger>
        <Trigger Property="IsError" Value="True">
            <Setter Property="BorderBrush" Value="{StaticResource SRed}"/>
        </Trigger>
        <Trigger Property="IsCorrect" Value="True">
            <Setter Property="BorderBrush" Value="{StaticResource SGreen}"/>
        </Trigger>
    </Style.Triggers>
</Style>

我的問題是,我想將一些復選框添加到列表框,並且在大多數情況下都可以使用。 但是,擴展屬性將被忽略,似乎ListBox只是將項目顯示為常規UIElement,而不是擴展版本。

<ListBox  ItemsSource="{Binding MyObjects}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <local:EBC_CheckBox Content="{Binding Path=Name}" IsChecked="{Binding Path=IsPresent}" IsError="{Binding Path=IsError}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如何使對象與擴展屬性一起顯示在列表框內(設置IsError時為紅色,等等)?

根據Damir的回答,如果我使用數據觸發器,則可以設置整個ListBoxItem的樣式(即,設置邊框可以在整個項目周圍設置邊框,而不僅僅是更改復選框的顏色)。

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsError}" Value="True">
            <Setter Property="BorderBrush" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

是否可以做這樣的事情並重用控件的格式,還是我需要用自定義的listboxitems創建一個自定義的列表框?

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsError}" Value="True">
            <Setter Property="local:EBC_CheckBox.IsError" Value="True"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

進一步編輯:

我發現以下代碼有效,它使用了固定的項目列表:

<ListBox HorizontalAlignment="Left" Height="100" Margin="72,383,0,0" Grid.Row="1" VerticalAlignment="Top" Width="100">
    <local:EBC_CheckBox Content="Item1" IsCorrect="True"/>
    <local:EBC_CheckBox Content="Item2" IsChecked="True"/>
    <local:EBC_CheckBox Content="Item3" IsError="True"/>
    <local:EBC_CheckBox Content="Item4" />
</ListBox>

該列表顯示項目,並且根據EBC_CheckBox設置正確處理了它們的視覺狀態。

如果我更改為綁定項的列表,盡管事物的視覺方面未正確更新-正確處理了Content和IsChecked字段(預期這些字段位於基本Checkbox中,但是IsError字段將被忽略)

<ListBox Grid.Column="3" Grid.Row="8" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="0"
         ItemsSource="{Binding Things}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <local:EBC_CheckBox Content="{Binding Path=ThingName}" IsChecked="{Binding Path=IsPresent}" IsError="{Binding Path=IsError}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我在想什么???

您必須使用DataTrigger而不是簡單的Trigger來正確綁定。 嘗試這個:

            <DataTrigger Binding="{Binding IsError}" Value="True">
                <Setter Property="BorderBrush" Value="Red" />
                <Setter Property="FontSize" Value="20"/>
            </DataTrigger>

解決問題后,一切都歸結為構造函數中依賴項屬性的設置。 這些設置不應該存在,在構造函數中進行設置會覆蓋應用程序中的值並限制其值。

另請參閱此問題

暫無
暫無

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

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