GetValue(NameProperty)返回""尽管“x: Name"for UI元素

本文关键字:quot Name for UI 元素 NameProperty 返回 GetValue 尽管 | 更新日期: 2023-09-27 18:11:39

我正在尝试实现这个https://stackoverflow.com/a/3754004/1805329

我的问题是

(string) column.GetValue (NameProperty)

返回",尽管我为我的网格中的每一列设置了名称。

<fw:DataGridEx x:Name="LogGrid"
                               Visibility="Visible"
                               DockPanel.Dock="Top"
                               VerticalScrollBarVisibility="Auto"
                               SelectionMode="Single"
                               SelectionUnit="FullRow"
                               IsReadOnly="True"
                               AlternatingRowBackground ="Gainsboro"
                               AlternationCount="2"
                               ItemsSource="{Binding Path=LogView, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
                               SelectedItem="{Binding Path=LogItem, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
                               HiddenColumns="{Binding LogHiddenColumns}"
                               AutoGenerateColumns="False">
                    <i:Interaction.Behaviors>
                        <fw:ScrollIntoViewBehavior/>
                    </i:Interaction.Behaviors>
                    <DataGrid.Columns>
                        <DataGridTextColumn x:Name="logColumnTime"
                                            Header="Time"
                                            Binding="{Binding Time}" />
                        <DataGridTextColumn x:Name="logColumnDirection"
                                            Header="Direction"
                                            Binding="{Binding Direction}" />
                        <DataGridTextColumn x:Name="logColumnData"
                                            Header="Data"
                                            Binding="{Binding Data}" />
                        <DataGridTextColumn x:Name="logColumnSpecifics"
                                            Header="Specifics"
                                            Binding="{Binding Specifics}" />
                        <DataGridTextColumn x:Name="logColumnOverheadData"
                                            Header="Overhead Data"
                                            Binding="{Binding OverheadData}" />
                        <DataGridTextColumn x:Name="logColumnOverheadSpecifics"
                                            Header="Overhead Specifics"
                                            Binding="{Binding OverheadSpecifics}" />
                    </DataGrid.Columns>
                </fw:DataGridEx>

当我用

设置列名时
columns[0].SetValue(NameProperty, "Time");

GetValue(NameProperty)方法返回正确的值。我显然不想这么做,因为这违背了MVVM模式。我这样做只是为了调试。

有谁知道为什么它不识别XAML中定义的名称吗?

GetValue(NameProperty)返回""尽管“x: Name"for UI元素

我找到了解决问题的方法。也请阅读最初的问题,并检查我提到我试图实现的链接。

DataGridTextColumn没有name属性。http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtextcolumn_properties (v = vs.110) . aspx

您必须通过实现自己的DataGridTextColumn来添加一个。

public class DataGridTextColumnEx : DataGridTextColumn
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value != _name)
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }
}

DataGridEx实现中的变化

foreach (DataGridTextColumnEx column in dg.Columns)
        {
            if (hiddenColumns.Contains((string)column.Name))
                column.Visibility = Visibility.Collapsed;
            else
                column.Visibility = Visibility.Visible;
        }

和XAML列条目更改

<fw:DataGridTextColumnEx Name="logColumnTime"
                         Header="Time"
                         Binding="{Binding Time}" />