繁体   English   中英

如何在Silverlight中从DataTemplate绑定到ItemsControl中的索引

[英]How to Bind to index in ItemsControl from DataTemplate in silverlight

我有一个正在使用ItemsControl的Silverlight应用程序。 在该ItemsControl内部,我有一个定义如下的DataTemplate:

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>

我有一个对象列表:

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;

TestObj类如下:

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

我想在控件中显示属性,例如:

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"

这个怎么做?

由于您要显示的OddType等于1或2或3,因此您只能有一个文本块。 好像选择了1,则TextBox之一将具有OF1,而其他将为null。

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

如果在这里您更新_OddFakctor,它将绑定文本框,并将值wat ur传递给该_oddfactor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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