2017-10-17 87 views
-1

我想綁定TextBox的背景顏色並使用轉換器。但是,由於TextBoxDataTemplateContentControl中,我找不到正確的綁定。在contentcontrol中綁定datatemplate的背景

DebuggingConverter不火,除非我設置爲Path=.

這裏從Path=StateColor綁定路徑是XAML:

<GridViewColumn Header="Value" Width="{Binding SelectedDeviceCol2, Source={x:Static properties:Settings.Default}, Mode=TwoWay}" > 
<GridViewColumn.CellTemplate> 
    <DataTemplate> 
     <ContentControl Content="{Binding Path=Value, Mode=TwoWay}" IsEnabled="{Binding Path=ReadOnly, Converter={StaticResource readOnlyToEnabledConverter}}"> 
      <ContentControl.Resources> 
       <DataTemplate DataType="{x:Type viewModels:VMDeviceCommand}"> 
        <Button Content="{Binding Name}" Height="24" IsEnabled="True" Click="Button_Click_GetObjectProperty"/> 
       </DataTemplate> 
       <DataTemplate DataType="{x:Type System:DateTime}"> 
        <DatePicker SelectedDate="{Binding Path=.}" 
          SelectedDateFormat="Short" 
          FirstDayOfWeek="Monday" 
          IsTodayHighlighted="True" > 
        </DatePicker> 
       </DataTemplate> 
       <DataTemplate DataType="{x:Type System:String}"> 
        <TextBox Text="{Binding Path=.}" TextChanged="TextBox_OnTextChanged"> 
         <TextBox.Background> 
          <SolidColorBrush Color="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=StateColor, Converter={StaticResource DebuggingConverter}}"/> 
         </TextBox.Background> 
        </TextBox> 
       </DataTemplate>     
       <DataTemplate DataType="{x:Type System:UInt16}"> 
        <xctk:IntegerUpDown Text="{Binding Path=.}" ValueChanged="UpDownBase_OnValueChanged" > 
        </xctk:IntegerUpDown> 
       </DataTemplate> 
       <DataTemplate DataType="{x:Type System:Boolean}"> 
        <CheckBox IsChecked="{Binding Path=.}" Click="CheckBox_Click"/> 
       </DataTemplate> 
      </ContentControl.Resources> 
     </ContentControl> 
    </DataTemplate> 
</GridViewColumn.CellTemplate> 

回答

1

ContentControl中沒有一個StateColor屬性,因此綁定從來沒有得到它的價值,並沒有任何東西傳遞給轉換器。

如果ContentControl中的DataContext的有StateColor屬性,這很簡單:

<SolidColorBrush 
    Color="{Binding DataContext.StateColor, RelativeSource={RelativeSource AncestorType=ContentControl}, Converter={StaticResource DebuggingConverter}}" 
    /> 
+1

這一工程....謝謝! – Web