简体   繁体   中英

How to Bind to index in ItemsControl from DataTemplate in silverlight

I have a Silverlight application that is using a ItemsControl. Inside of that ItemsControl I have a DataTemplate that is defined like the following:

XAML

<ItemsControl Grid.Row="1" Grid.ColumnSpan="5" x:Name="dgOdds">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid x:Name="gRoot">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="200"/>
                                <ColumnDefinition Width="200"/>
                                <ColumnDefinition Width="200"/>
                            </Grid.ColumnDefinitions>

                            <TextBox x:Name="OF1" Grid.Column="0" Text="{Binding OddFactor, Mode=TwoWay}" FontWeight="Bold" VerticalAlignment="Center" HorizontalContentAlignment="Center"/>
                            <TextBox x:Name="OFX" Grid.Column="1" Text="{Binding OddFactor, Mode=TwoWay}" FontWeight="Bold" VerticalAlignment="Center" HorizontalContentAlignment="Center"/>
                            <TextBox x:Name="OF2" Grid.Column="2" Text="{Binding OddFactor, Mode=TwoWay}" FontWeight="Bold" VerticalAlignment="Center" HorizontalContentAlignment="Center"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

I have a list of objects:

C#

ObservableCollection<TestObj> SomeList = new ObservableCollection<TestObj>;
SomeList.Add(new TestObj(){ OddType = 1, OddFakctor = 1.1 });
SomeList.Add(new TestObj(){ OddType = 2, OddFakctor = 2.2 });
SomeList.Add(new TestObj(){ OddType = 3, OddFakctor = 3.3 });
this.dgOdds.ItemsSource = this.Collection;

The TestObj class is as follows:

public class TestObj
    {
        public double OddType { get; set;}
        public double OddFakctor { get; set; }
    }

I want to display the properties in my controls something like:

If OddType equal 1, than show in TextBox x:Name="OF1", 
if OddType equal 2, than show in TextBox x:Name="OFX", 
if OddType equal 3, than show in TextBox x:Name="OF3"

How to do this?

since u want to show the OddType equals to 1 or 2 or 3, u can have only a single textblock. As if 1 is selected, then one of the TextBox would have OF1 and others will be null.

private double _oddfactor;
public double OddFakctor 
 {
        get
        {
            return _oddfactor;
        }
        set
        {
            _oddfactor = value;
            OnPropertyChanged(() => OddFakctor);
        }
    }

if here u update the _OddFakctor , it will bind the text box with the value wat ur passing to that _oddfactor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM