Lỗi khi sử dụng controltemplate và liên kết với nhau trong lưới dữ liệu hộp văn bản WPF

lập trình


Xin chào, tôi có DataGrid. Tôi đã sử dụng TextBox trong các ô của nó.
Tôi đã tạo ControlTemplate cho nó.
Vì vậy, vấn đề của tôi là khi tôi sử dụng controlTemplate, Binding của TextBox sẽ không hoạt động. Nhưng nếu tôi trực tiếp đặt mọi thứ trong DataTemplate thì Binding sẽ hoạt động hoàn hảo.
Bất kỳ ý tưởng nào xin vui lòng?

Những gì tôi đã thử:

Liên kết với TextBox hoạt động hoàn hảo trong mã này:

XML
<DataGridTemplateColumn Header="Text" IsReadOnly="False" Width="38*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox 
                Text="{Binding Path=TheText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                Background="Transparent"
                Foreground="White"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Liên kết với TextBox không hoạt động trong mã này !!! :

XML
<DataGridTemplateColumn Header="Text" IsReadOnly="False" Width="38*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox 
                Text="{Binding Path=TheText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                Template="{DynamicResource TextBoxOfCellsInDataGridOfSubtitle}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Giải pháp 1

Tôi đã tìm thấy câu trả lời.
Tôi nên liên kết Văn bản với cha mẹ, sau đó liên kết với Lớp.

XML
<DataGridTemplateColumn Header="Text" IsReadOnly="False" Width="38*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox 
                Text="{Binding Path=TheText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                Template="{DynamicResource TextBoxOfCellsInDataGridOfSubtitle}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

XML
<ControlTemplate x:Key="TextBoxOfCellsInDataGridOfSubtitle" TargetType="TextBox">
    <TextBox Text="{TemplateBinding Text}"
             AcceptsReturn="False" 
             CaretBrush="White" 
             Foreground="White"
             Background="Transparent"
             BorderBrush="Transparent"
             BorderThickness="0"
             TextAlignment="Center"
             HorizontalAlignment="Stretch"
             HorizontalContentAlignment="Stretch"
             VerticalAlignment="Stretch"
             VerticalContentAlignment="Center"/>
</ControlTemplate>

コメント

タイトルとURLをコピーしました