gpt4 book ai didi

c# - 尽管在XAML中为UI元素设置了 “”,但GetValue(NameProperty)返回 “x:Name”

转载 作者:行者123 更新时间:2023-12-03 10:46:18 24 4
gpt4 key购买 nike

我正在尝试实现这个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中定义的名称?

最佳答案

我找到了解决我问题的方法。还请阅读最初的问题,并检查我提到要实现的内容的链接。

DataGridTextColumn没有名称属性。 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}" />

关于c# - 尽管在XAML中为UI元素设置了 “”,但GetValue(NameProperty)返回 “x:Name”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24607356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com