【问题标题】:Data bind element name of another control inside datatemplate of a datagridtemplatecolumn, and the converter is not firing数据网格模板列的数据模板内另一个控件的数据绑定元素名称,并且转换器未触发
【发布时间】:2012-02-05 10:56:39
【问题描述】:
<UserControl.Resources>

            <local:ColorConverter x:Key="cc"></local:ColorConverter>

</UserControl.Resources>
<sdk:DataGrid x:Name="gridAllContacts"  Grid.Row="1" Grid.Column="0" Width="500"                   Height="300"  AutoGenerateColumns="False">

      <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn Header="Name">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                   <StackPanel>
                    <TextBox Text="{Binding Path=Name, Mode=TwoWay}" Background="{Binding ElementName=columns2, Path=SelectedIndex, Mode=TwoWay, Converter={StaticResource cc}}" ></TextBox>
                   </StackPanel>
                </DataTemplate>
           </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
     </sdk:DataGrid.Columns>              

 </sdk:DataGrid>
 <ComboBox Name="columns2"  VerticalAlignment="Center" Height="25" HorizontalAlignment="Left" Margin="150,0,0,0" SelectedIndex="0">
    <ComboBoxItem>Name</ComboBoxItem>
    <ComboBoxItem>School</ComboBoxItem>
    <ComboBoxItem>ContactID</ComboBoxItem>
</ComboBox>

特定列的数据模板内文本框的背景应根据所选的 ComboBox 项着色。但是现在,当我在转换器类中放置一个断点时,转换器根本没有触发。我在某处读到您需要 DataContextProxy,如果需要,我该怎么做?

【问题讨论】:

    标签: c# silverlight xaml data-binding datagrid


    【解决方案1】:

    您可以尝试将组合框移动到 UserControl 的资源中(将名称更改为 x:Key)。

    然后用 ContentPresenter 替换原来的组合框位置,其内容是资源中的组合框。

    datagrid的绑定需要改成Background="{Binding Source={StaticResource comboBoxKeyInResources}", Path=SelectedIndex...

    这样转换器绑定应该可以工作。

    【讨论】:

      【解决方案2】:

      我这样做是对的,这正是我想要的方式:

              DataGridCell tempCell = new DataGridCell();
      
              Style cellStyle = new Style();
              cellStyle.TargetType = tempCell.GetType();
              cellStyle.Setters.Add(new Setter(DataGridCell.ForegroundProperty, new SolidColorBrush(Colors.Red)));
              gridAllContacts.Columns[0].CellStyle = cellStyle;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-01
        • 2013-09-26
        • 1970-01-01
        • 1970-01-01
        • 2014-09-07
        • 2018-06-08
        • 1970-01-01
        • 2011-04-26
        相关资源
        最近更新 更多