動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
string型Mynumberに対して数値ではない場合にsetしないようにしてみる。
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 _170612_t1540_isNumeric
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var data = new List<myList>();
data.Add(new myList { Mynumber = "314" });
data.Add(new myList { Mynumber = "pi" });
data.Add(new myList { Mynumber = "2718" });
uxMyNumbet.ItemsSource = data;
}
}
public class myList
{
private string mynumber;
public string Mynumber { get { return mynumber; }
set {
if (Int32.TryParse(value, out Int32 wrk) == false)
{
return;
}
mynumber = wrk.ToString();
}
}
}
}
MainWindow.xaml
<Window x:Class="_170612_t1540_isNumeric.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:_170612_t1540_isNumeric"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="uxMyNumbet" Height="250"
DisplayMemberPath="Mynumber"/>
</Grid>
</Window>
数値でない("pi")ところは""になってしまった。