簡體   English   中英

如何向我的自定義列表框添加內容?

[英]How can I add something to my custom listbox?

我制作了一個自定義列表框,但是當我按下按鈕顯示每種語言時,什么也沒有顯示。 我怎樣才能解決這個問題。 當我刪除列表框樣式時,它工作正常。 都在C#,希望大家幫幫我。

問候埃利亞斯

<ListBox x:Name="lsbResultaatTaal"
             Foreground="Black"
             FontSize="15"
             Grid.Row="1"
             Margin="528,56,0,0" 
             Height="450" 
             VerticalAlignment="Top" 
             HorizontalAlignment="Left" 
             Width="233">
    

    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{d:SampleData ItemCount=5}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>

    <ListBox.Style>
        <Style TargetType="ListBox">
            <Setter Property="Background" Value="Gray"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Gray"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>

    <ListBox.Template>
        <ControlTemplate TargetType="ListBox">
            <Border Width="230" Height="450"
                        CornerRadius="9"
                        BorderThickness="1"
                        BorderBrush="Black"
                        Background="{TemplateBinding Background}">
                <ContentPresenter VerticalAlignment="Center"
                                      HorizontalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </ListBox.Template>
</ListBox>

您可以在資源中設置樣式

 <Window.Resources>
    <Style TargetType="{x:Type ListBox}">
        <Setter Property="Background" Value="Gray" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Gray" /> 
                <!-- 
                     Why you choose same background color when hover listbox 
                     Maybe same color your background and foreground
                -->
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

或者

您可以嘗試以下更改嗎:

<DataTemplate>
    <TextBlock Text="{Binding}" />
</DataTemplate>


<ContentPresenter
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Content="{Binding YourProp}" />

暫無
暫無

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

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