Visual Studio/WPF > ObjectDataProvider > 홀수 및 짝수 인덱스 난수의 ListBox 표시 > IEnumerable 함수에 인수 추가 | ObjectDataProvider.MethodParameters 지정 | xmlns:system 정의 | system:Boolean 사용

운영 환경
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

h tp : /// s ぇ l. ldb㎉g. jp / archi s / 52333750. HTML
를 참고로 ObjectDataProvider 를 사용해 작성 클래스의 함수를 사용하는 예를 공부중.
인수를 사용하는 예를 검토해 보았다.
  • IEnumerable 함수에 인수 추가
  • ObjectDataProvider.MethodParameters를 지정
  • xmlns:system 정의
  • system:Boolean 사용

  • v0.1



    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(bool isOdd)
            {
                int start = isOdd ? 1 : 0;
                for (int idx = start; idx < 12 * 2; idx+=2)
                {
                    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"
            xmlns:system="clr-namespace:System;assembly=mscorlib"
            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_odd">
                 <ObjectDataProvider.MethodParameters>
                    <system:Boolean>True</system:Boolean>
                </ObjectDataProvider.MethodParameters> 
            </ObjectDataProvider>
            <ObjectDataProvider ObjectType="{x:Type local:NumberUtil}"
                                MethodName="GetIndexNumbers" x:Key="IndexNum_even">
                <ObjectDataProvider.MethodParameters>
                    <system:Boolean>False</system:Boolean>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>
        <Grid>
            <StackPanel Orientation="Horizontal">
                <ListBox Height="300" Margin="5"
                     ItemsSource="{Binding Mode=OneWay, Source={StaticResource IndexNum_odd}}"/>
                <ListBox Height="300" Margin="5"
                     ItemsSource="{Binding Mode=OneWay, Source={StaticResource IndexNum_even}}"/>
                <ListBox Height="300" Margin="5"
                     ItemsSource="{Binding Mode=OneWay, Source={StaticResource RandomNum}}"/>
            </StackPanel>
        </Grid>
    </Window>
    



    XAML은 MethodParameters를 마크 업 확장으로 줄 수 있다면 더 컴팩트하게 될 것입니다.

    좋은 웹페이지 즐겨찾기