kformeck
kformeck

Reputation: 1823

Binding a ListBox's SelectedItem property to two separate TextBoxs

So I have a list box that will contain a list of uninspected die prints. The list box contains a list of strings; a die print has the naming convention of "'row'x'col'".

Example: 02x09, 05x06 etc.

I also have two text boxes that allow the user to manually type in the die print they want to move to, but instead of one box for the entire string, its separate in to a row text box and a column text box.

Example: txtRow x txtCol; 02 in row text box and 09 in the column text box takes you to 02x09.

I want the user to also be able to select a print from the uninspected die prints list box and be able to load that map from there. The easiest way to do it will be to bind the SelectedItem property of the list box to the two (row,col) text boxes. This is easy because all the pluming is already done for mapping a die print when the user enters a row and column coordinate of a print.

The big question:

How can a bind two text boxes to a single SelectedItem property of a list box?

in other words,

If the current SelectedItem in the list box is "02x09", how can I bind the "02" to the row text box and the "09" to the column text box?

Upvotes: 0

Views: 298

Answers (1)

JSJ
JSJ

Reputation: 5691

I recomand you to use element binding and converter to convert value from 02x09. so your one textbox will have first halp and second will have other half.

here is a sample code. for you.

<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();
        }
    }

Upvotes: 1

Related Questions