2020년 10월 10일 토요일

wpf test

MainWindow.xaml 

<Window x:Class="WpfApplication1.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:WpfApplication1"

        mc:Ignorable="d"

        Title="MainWindow" Height="150" Width="900">

    <Window.Resources>

        <local:MYViewModel x:Key="myVM"/>

    </Window.Resources>

    <Grid  DataContext="{Binding Source={StaticResource myVM}}" Width="500" HorizontalAlignment="Left">

        <ItemsControl ItemsSource="{Binding ThingsList, Mode= OneWay}"  >

            <ItemsControl.ItemsPanel>

                <ItemsPanelTemplate>

                    <WrapPanel/>

                </ItemsPanelTemplate>

            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate >

                <DataTemplate>

                    <!--<TextBlock Text="{Binding Name}" Foreground="{Binding Color}" TextAlignment="Left"/>-->

                    <TextBlock TextAlignment="Left">

                        <Run Text="{Binding Name}" Foreground="{Binding Color}" />

                    </TextBlock>

                </DataTemplate>

            </ItemsControl.ItemTemplate>

        </ItemsControl>

    </Grid>

</Window>

-----------------------------------------------

MainWindow .xaml.cs

using System;

using System.Collections.Generic;

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 WpfApplication1

{

    /// <summary>

    /// MainWindow.xaml の相互作用ロジック

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

    }

}

------------------------------------------------

MainWindow.cs


using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace WpfApplication1

{

    public class NamedThing

    {

        public string Name { get; set; }

        public string Color { get; set; }

    }


    public class MYViewModel

    {

        public List<NamedThing> ThingsList { get; set; }

             = new List<NamedThing>

            {

            new NamedThing {Name = "Short1",Color= "#FF0000"},

            new NamedThing {Name = ", ", Color ="black"},

            new NamedThing {Name = "Short2", Color= "red"},

            new NamedThing {Name = ", ", Color ="black"},

            new NamedThing {Name = "Short3", Color= "blue"},

            new NamedThing {Name = ", ", Color ="black"},

            new NamedThing {Name = "Short4", Color= "red"}

            };

    }

}




댓글 없음:

댓글 쓰기