首页 > 其他分享 >WPF DataTemplate DataTrigger

WPF DataTemplate DataTrigger

时间:2024-05-02 14:45:01浏览次数:15  
标签:days DataTrigger Windows System public using WPF rnd DataTemplate

<Window x:Class="WpfApp79.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp79"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:GeneralForecastToBrushConverter x:Key="gf2brush"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Select number of days to forecast:"
                       FontSize="15" VerticalAlignment="Center" Margin="4"/>
            <ComboBox x:Name="_days" SelectedIndex="0" Width="50"/>
            <Button Margin="4" Content="Get Forecast" FontSize="16" Click="Button_Click" />
        </StackPanel>
        <StackPanel Orientation="Horizontal" Grid.Row="1" Margin="5"
                    TextBlock.FontSize="15">
            <TextBlock Text="Select units:" Margin="4"/>
            <RadioButton Content="Celsius" IsChecked="True"
                         Margin="10,5"/>
            <RadioButton Content="Fahrenheit" Margin="20,5"/>
        </StackPanel>
        <ListBox ItemsSource="{Binding}" Grid.Row="2" 
                 HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border x:Name="_border" Margin="5" BorderBrush="Black" Padding="5"
                            BorderThickness="2"  
                            Background="LightGray">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontSize="20" FontWeight="ExtraBold"
                                       Text="{Binding GeneralForeCastValue}" 
                                       Background="{Binding GeneralForecast,Converter={StaticResource gf2brush}}"/>
                            <TextBlock FontSize="16" Margin="10,0,0,0"
                                       VerticalAlignment="Bottom"
                                       Text="{Binding TemperatureLow,StringFormat='Low:\{0:N2\}'}"/>
                            <TextBlock FontSize="16" Margin="10,0,0,0"
                                       VerticalAlignment="Bottom"
                                       Text="{Binding TemperatureHigh,StringFormat='High:\{0:N2\}'}"/>
                            <TextBlock FontSize="16" Margin="20,0,0,0"
                                       VerticalAlignment="Bottom"
                                       Text="{Binding Percipitation,StringFormat='Percip:\{0:N2\}'}"/>
                        </StackPanel>
                    </Border>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding GeneralForeCastValue}" Value="Sunny">
                            <Setter Property="Background" Value="Yellow" TargetName="_border"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding GeneralForeCastValue}" Value="Snowy">
                            <Setter Property="Background" Value="LightBlue" TargetName="_border"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding GeneralForeCastValue}" Value="Rainy">
                            <Setter Property="Background" Value="LightGreen" TargetName="_border"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding GeneralForeCastValue}" Value="Dry">
                            <Setter Property="Background" Value="LightYellow" TargetName="_border"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp79
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            _days.ItemsSource = Enumerable.Range(1, 30);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var data = new List<Forecast>();
            int days = (int)_days.SelectedItem;
            var rnd = new Random();
            for (int i = 0; i < days; i++)
            {
                double temp = rnd.NextDouble() * 40 - 10;
                var forecast = new Forecast
                {
                    GeneralForeCastValue = (GeneralForecast)rnd.Next(Enum.GetValues(typeof(GeneralForecast)).Length),
                    TemperatureLow = temp,
                    TemperatureHigh = temp + rnd.NextDouble() * 15,
                    Percipitation = rnd.Next(10) > 5 ? rnd.NextDouble() * 10 : 0
                };
                data.Add(forecast);
            }
            DataContext = data;

        }
    }

    public class Forecast
    {
        public GeneralForecast GeneralForeCastValue { get; set; }
        public double TemperatureHigh { get; set; }
        public double TemperatureLow { get; set; }
        public double Percipitation { get; set; }
    }

    public enum GeneralForecast
    {
        Sunny,
        Rainy,
        Snowy,
        Cloudy,
        Dry
    } 
}

 

标签:days,DataTrigger,Windows,System,public,using,WPF,rnd,DataTemplate
From: https://www.cnblogs.com/Fred1987/p/18170178

相关文章

  • WPF多语言支持:简单灵活的动态切换,让你的程序支持多国语言
     概述:本示例演示了在WPF应用程序中实现多语言支持的详细步骤。通过资源字典和数据绑定,以及使用语言管理器类,应用程序能够在运行时动态切换语言。这种方法使得多语言支持更加灵活,便于维护,同时提供清晰的代码结构。在WPF中实现多语言的一种常见方法是使用资源字典和数据绑定。......
  • WPF DataTemplate DataTemplateSelector
    //xaml<Windowx:Class="WpfApp78.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF DataTemplate DataType
    //xaml<Windowx:Class="WpfApp77.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF SetProperty to implement compare,assign and notify
    protectedvoidSetProperty<T>(refTfield,Tvalue,[CallerMemberName]stringpropName=null){if(!EqualityComparer<T>.Default.Equals(field,value)){field=value;varhandler=PropertyChanged;if(handler!=nul......
  • WPF MVVM Datagrid Selected Multiple items via behavior interaction.trigger,event
    1.Install Microsoft.Xaml.Behaviors.WpffromNuget;2.Addbehaviorreferenceinxamlxmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"3.Passmethodtomvvmviabehavior,interaction,trigger,eventname,TargetObject,MethodNameinxaml......
  • WPF 后台设置DataGrid选中多行
    1privatevoidSetSelectIndex(List<int>listIndex)2{3try4{5foreach(variinlistIndex)6{7if(i>=datagridSig.Items.Count)8......
  • WPF pass event method to viewmodel via Interaction:CallMethodAction,TargetObject
    <Windowx:Class="WpfApp71.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • 【转】[WPF] 复制文本到剪贴板
    来自:阿里的通义灵码以下是几种常见的复制数据类型到剪切板的方法:复制文本到剪切板usingSystem.Windows.Forms;//对于WindowsForms应用//或者usingSystem.Windows;//对于WPF应用publicvoidCopyTextToClipboard(stringtext){//确保在UI线程中操作剪切板......
  • WPF & Prism
    WPF编程-Prism世有伯乐,然后有千里马。千里马常有,而伯乐不常有。一、背景Winform和WPF1.WinForms和WPF技术架构:WinForms是基于传统的窗体和控件的技术,使用的是类似于VB6时代的设计理念。WPF是基于XAML(可扩展应用程序标记语言)的技术,允许更灵活、高度可定制化......
  • WPF所有原生空间使用demo
    //前台窗体<Windowx:Class="WpfTestDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.......