You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF中DataTrigger内TextBox绑定MyItem的Name属性失败求助

问题描述

以下代码无法正常工作:

<TextBox Text="{Binding Path=Name}"/>

无法在DataTrigger内部创建与MyItem类的简单绑定,该绑定在触发器外部可正常运行。是否需要添加额外的绑定信息?

相关代码如下:

ViewModel与MyItem类

public class ViewModel: INotifyPropertyChanged {

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public ObservableCollection<MyItem> MyItems {
        get;
        set;
    }
    public ViewModel()
    {
        MyItems = new ObservableCollection<MyItem>() {
            new MyItem() { Name="1", ControlType="Button" },
            new MyItem() { Name="2", ControlType="CheckBox" },
            new MyItem() { Name="3", ControlType="TextBox" }
        };
    }
}

public class MyItem : INotifyPropertyChanged {

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private string name;

    public string Name {
        get { 
            return name; 
        }
        set {
            if (value != name) {
                name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    private string controlType;
    public string ControlType { get => controlType; set => controlType = value; }
}

XAML代码

<DataGrid ItemsSource="{Binding MyItems}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Control Type" Binding="{Binding ControlType}"/>
            
        <DataGridTemplateColumn Header="Actual Control">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    
                    <ContentControl>
                        <ContentControl.Style>
                            <Style TargetType="ContentControl">
                                <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=ControlType}" Value="TextBox">
                                    <Setter Property="ContentTemplate">
                                        <Setter.Value>
                                            <DataTemplate>
                                                <TextBox Text="Test"/>
                                                <!--   无法工作的绑定 <TextBox Text="{Binding Path=Name}"/>--> 
                                            </DataTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ContentControl.Style>
                    </ContentControl>
                    
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
解决方案

问题核心是ContentControl未绑定数据源:ContentTemplate的DataContext默认指向ContentControl的Content属性,而你没有设置该属性,导致模板内的绑定找不到MyItem实例。

只需给ContentControl添加Content="{Binding}",让它绑定到当前行的MyItem对象,模板内的绑定即可正常生效:

<DataGrid ItemsSource="{Binding MyItems}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Control Type" Binding="{Binding ControlType}"/>
            
        <DataGridTemplateColumn Header="Actual Control">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <!-- 绑定Content到当前MyItem实例 -->
                    <ContentControl Content="{Binding}">
                        <ContentControl.Style>
                            <Style TargetType="ContentControl">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=ControlType}" Value="TextBox">
                                        <Setter Property="ContentTemplate">
                                            <Setter.Value>
                                                <DataTemplate>
                                                    <!-- 绑定现在可以正常工作 -->
                                                    <TextBox Text="{Binding Path=Name}"/>
                                                </DataTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ContentControl.Style>
                    </ContentControl>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

额外建议:给MyItem的ControlType属性添加PropertyChanged通知,确保DataTrigger能响应属性变化:

private string controlType;
public string ControlType 
{ 
    get => controlType; 
    set 
    {
        if (controlType != value)
        {
            controlType = value;
            OnPropertyChanged(nameof(ControlType));
        }
    }
}

内容的提问来源于stack exchange,提问作者Rolf Peters

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 14:20:30