2011-03-24 176 views
1

我需要通過轉換器設置項目的Grid.Column屬性。 這是我的xaml:WPF綁定Grid.Column屬性

<ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding Periodo.Inizio}"> 
      <Grid.Column> 
       <MultiBinding Converter="{StaticResource ItemColumnSetter}"> 
        <Binding RelativeSource="{RelativeSource Self}" /> 
        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="StartDate" /> 
        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="EndDate" /> 
        <Binding Path="Periodo.Inizio" /> 
       </MultiBinding> 
      </Grid.Column> 
     </TextBlock> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 

但是不行。 我確信轉換器工作正常...

+0

你忘了添加代碼示例。 – 2011-03-24 12:36:09

+0

對不起,發佈格式錯誤 – 2011-03-24 12:40:24

回答

4

您的TextBlock將被封裝在某種控件中,這意味着任何Grid.XXX屬性都將被忽略。要正確應用這些,您需要在ItemsControl.ItemContainerStyle中進行綁定。

應該是這樣的:

<ItemsControl.ItemContainerStyle> 
    <Style> 
     <Setter Property="Grid.Column"> 
      <Setter.Value> 
       <MultiBinding Converter="{StaticResource ItemColumnSetter}"> 
        <Binding RelativeSource="{RelativeSource Self}" /> 
        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="StartDate" /> 
        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="EndDate" /> 
        <Binding Path="Periodo.Inizio" /> 
       </MultiBinding> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ItemsControl.ItemContainerStyle> 
+0

好的,它的工作...謝謝 – 2011-03-24 12:48:59

+0

,但不適用ColumnSpan ... – 2011-03-24 13:13:18

+0

你使用這樣的setter?即''Setter Property =「Grid.ColumnSpan」> ...'它應該可以工作,也許你的綁定或轉換器有問題? – 2011-03-24 13:16:41