DataGrid 数据绑定样式问题
我有一个 WPF DataGrid,我想对所有单元格应用文本换行,所以我定义了这种样式:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" FontSize="15" Text="{Binding}" VerticalAlignment="Center"
HorizontalAlignment="Center" ></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="95" />
<DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="95" />
<DataGridTextColumn Header="Category" Binding="{Binding Category}" Width="95" />
</DataGrid.Columns>
并且我在后面的代码中设置了 DataGrid 的 ItemsSource,如下所示:
myDataGrid.ItemsSource= new Customers[]
{
new ComputerStandard{Name="Michael Thomas",Address="16 Greenville Avenue",Category="A"},
new ComputerStandard{Name="Fiona Thompson",Address="19 Wright Street",Category="F"},
new ComputerStandard{Name="Jack Smith",Address="133 Kensington Road",Category="B"},
new ComputerStandard{Name="Michael jackson",Address="11 Wine Street",Category="C"},
new ComputerStandard{Name="Jerry Holmes",Address="10 Awson Street",Category="G"},
new ComputerStandard{Name="David Philips",Address="Not Specified",Category="A"}
};
但是在某些地方,我设置的绑定表达式失败了我的风格 Text="{Binding}"
最后得到:
显然绑定表达式Text="{Binding}"
失败了,我知道这一点,因为当我删除样式时,一切都工作正常。我该如何解决这个问题?
提前致谢。
I have a WPF DataGrid and I want to apply text wrapping to all the cells, so I've defined this style:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" FontSize="15" Text="{Binding}" VerticalAlignment="Center"
HorizontalAlignment="Center" ></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="95" />
<DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="95" />
<DataGridTextColumn Header="Category" Binding="{Binding Category}" Width="95" />
</DataGrid.Columns>
and I set my DataGrid's ItemsSource in my code behind like this:
myDataGrid.ItemsSource= new Customers[]
{
new ComputerStandard{Name="Michael Thomas",Address="16 Greenville Avenue",Category="A"},
new ComputerStandard{Name="Fiona Thompson",Address="19 Wright Street",Category="F"},
new ComputerStandard{Name="Jack Smith",Address="133 Kensington Road",Category="B"},
new ComputerStandard{Name="Michael jackson",Address="11 Wine Street",Category="C"},
new ComputerStandard{Name="Jerry Holmes",Address="10 Awson Street",Category="G"},
new ComputerStandard{Name="David Philips",Address="Not Specified",Category="A"}
};
But somewhere something fails with my binding expression that I set in my style Text="{Binding}"
and I end up with:
Obviously the binding expression Text="{Binding}"
is failing, I know this because when I remove the style everything works perfectly. How do I go about fixing this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置
DataGridColumn.Binding
属性不会为每个DataGridCell
设置DataContext
。DataContext
仍然等于整个 Row 的DataContext
从绑定
ContentTemplate
切换到绑定Template
,并且然后您就可以访问ContentPresenter
Setting the
DataGridColumn.Binding
property does not set theDataContext
for eachDataGridCell
. TheDataContext
is still equal to the entire Row'sDataContext
Switch from binding the
ContentTemplate
, to binding theTemplate
, and then you have access to theContentPresenter
这将有所帮助:
摘自此处。
This will help :
Taken from here.