如何在WPF中设置列表框的模板(Windows 10天气)

tog*_*rli 2 c# wpf templates listbox listboxitem

我正在尝试使用创建一个Windows 10天气应用程序。我需要一个来显示最近 10 天的天气部分。我必须为项目设置模板。我试过这个:WPFC#ListboxListbox

<ListBox Grid.Row="1" x:Name="DailyWeatherListBox">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel>
                <!--...-->
            </StackPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

这是最近10天的天气部分(Windows 10天气)

查看 Windows 10 天气。我将此图像放置在 Windows 10 中以废弃。我也不知道如何设置ScrollbarListbox角落。如果您能提供帮助,我将非常感激。

Sii*_*aas 5

我会开始这样的事情:

<ListBox ItemsSource="{Binding WeeklyWeather}"
         SelectedItem="{Binding SelectedDailyWeather, Mode=TwoWay}">

    //Use ItemTemplate to set how item looks "inside"
    //I'll leave design details for you
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Day}"/>
                <Image Source={Binding WheatherPicturePath}/>
                <TextBlock Text="{Binding Temperature}"/>
                <TextBlock Text="{Binding Description}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

    //ItemsPanel defines container for items. It can be StackPanel, Wrapanel, Grid, etc
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        //You use this place to design how container normally looks like
                        <Border Background="White">
                            //DataTemplate defined above is placed in ContentPresenter
                            <ContentPresenter />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                //Here we catch "IsSelected" event and re-design our template to visually reflect "selected"
                <Trigger Property="IsSelected" Value="true">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border Background="Gray">
                                    <ContentPresenter />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>

</ListBox>
Run Code Online (Sandbox Code Playgroud)

以下是如何进行这些绑定的一些想法。

public class WeatherViewModel
{
    public string Day { get; set; }
    public string WheatherPicturePath { get; set; }
    public string Temperature { get; set; }
    public string Description { get; set; }
}

public class BindedDataContext
{
    public ObservableCollection<WeatherViewModel> WeeklyWeather { get; set; }
    public WeatherViewModel SelectedDailyWeather { get; set; }
    //...
}
Run Code Online (Sandbox Code Playgroud)

您的代码隐藏方法可能有所不同,但它们需要就位以便您使用这些绑定。

对于这样的滚动条,我会研究更改列表框中的滚动查看器模板