Visual Studio / WPF > ObjectDataProvider > インデックス付き乱数のListBox表示 | IEnumerable | yield return
8039 ワード
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2
http://gushwell.ldblog.jp/archives/52333267.html
を参考にObjectDataProvider を用いて作成クラスの関数を使う例を勉強中。
インデックスをつけてみた。
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 _170606_t1333_ObjectDataProvider
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class NumberUtil
{
public IEnumerable<int> GetRandomNumbers()
{
var rnd = new Random();
for (int idx=0; idx<12; idx++)
{
yield return rnd.Next(100, 1000);
}
}
public IEnumerable<int> GetIndexNumbers()
{
for (int idx = 0; idx < 12; idx++)
{
yield return idx;
}
}
}
}
MainWindow.xaml
<Window x:Class="_170606_t1333_ObjectDataProvider.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:_170606_t1333_ObjectDataProvider"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider ObjectType="{x:Type local:NumberUtil}"
MethodName="GetRandomNumbers" x:Key="RandomNum"/>
<ObjectDataProvider ObjectType="{x:Type local:NumberUtil}"
MethodName="GetIndexNumbers" x:Key="IndexNum"/>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<ListBox Height="300"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource IndexNum}}"/>
<ListBox Height="300" Margin="5"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource RandomNum}}"/>
</StackPanel>
</Grid>
</Window>
Author And Source
この問題について(Visual Studio / WPF > ObjectDataProvider > インデックス付き乱数のListBox表示 | IEnumerable | yield return), 我々は、より多くの情報をここで見つけました https://qiita.com/7of9/items/a0840d14f75bc5d5f1e7著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .