2017-10-17 84 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