2014-10-27 50 views
0

我有一个使用ItemsControl的Silverlight应用程序。里面说的ItemsControl的我有一个类似如下定义一个DataTemplate:如何绑定到ItemsControl中的DataTemplate中的索引silverlight

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" 

如何做到这一点?

回答

0

因为你想显示OddType等于1或2或3,你可以只有一个文本块。 就像选择了1,那么TextBox中的一个将具有OF1,而其他的将为空。

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

如果在这里-u更新的_OddFakctor,它将与笏ü[R传递到_oddfactor

+0

谢谢值的文本框绑定,但我不能每次都通过集合。 XAML如何做到这一点? – romanK 2014-10-27 08:46:32

+0

我编辑了答案。 请恢复为wat,以及如何访问您的代码中的textblock不xaml – 2014-10-27 08:57:28

相关问题