所以我有一个列表框,其中包含未经检查的模具打印列表。列表框包含字符串列表;模具印刷具有“'row'x'col'”的命名惯例。
示例:02x09,05x06等
我还有两个文本框,允许用户手动输入他们想要移动到的骰子打印,但不是整个字符串的一个框,而是分成行文本框和列文本框。
示例:txtRow x txtCol;行文本框中的02和列文本框中的09将带您进入02x09。
我希望用户也能够从未经检查的模具打印列表框中选择打印,并能够从那里加载该地图。最简单的方法是将列表框的SelectedItem属性绑定到两个(row,col)文本框。这很容易,因为当用户输入打印的行和列坐标时,已经完成了用于映射骰子打印的所有pluming。
最大的问题:
如何将两个文本框绑定到列表框的单个SelectedItem属性?
换句话说,
如果列表框中的当前SelectedItem是“02x09”,如何将“02”绑定到行文本框,将“09”绑定到列文本框?
答案 0 :(得分:1)
我重新命令你使用元素绑定和转换器来转换02x09的值。所以你的一个文本框会有第一个halp而第二个文本框会有另一半。
这是一个示例代码。对你而言。
<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:WPFTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<converters:MyConverter x:Key="converter"/>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Items}" Margin="0,0,360,0" x:Name="list">
</ListBox>
<TextBox Text="{Binding Path=SelectedValue,Converter={StaticResource converter},ConverterParameter=0, ElementName=list}" Height="25" Width="100"/>
<TextBox Text="{Binding Path=SelectedValue,Converter={StaticResource converter}, ConverterParameter=1,ElementName=list}" Height="25" Width="100" Margin="208,189,209,106"/>
</Grid>
</Window>
public partial class MainWindow : Window
{
public List<string> Items { get; set; }
public MainWindow()
{
InitializeComponent();
if (Items == null)
Items = new List<string>();
Random ran = new Random();
for (int i = 0; i < 10; i++)
{
Items.Add(ran.Next(1, 5) + "x" + ran.Next(5, 10));
}
this.DataContext = this;
}
}
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return "Null";
var values = value.ToString().Split(new string[] { "x" }, StringSplitOptions.None);
int x = 0;
if (int.TryParse(parameter.ToString(), out x))
{
return values[x].ToString();
}
return "N/A";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}