我有一个关于WPF中数据绑定DataGrid的问题.我使用的是VS 2010 Beta 2,它有自己的DataGrid,而不是Toolkit,虽然我认为它几乎是一样的.
我想绑定到一个有52列的数据集,一年中每周一个.出于这个原因,我想动态绑定数据而不是指定每个字段.每个字段的值都是真或假,具体取决于某些条件.基于此值,我想在条件为真时在单元格模板中显示图像,如果条件不为真,则隐藏它.
我的问题是我找到的所有使用模板的例子都是指固定的预定义字段,你可以使用Text ="{Binding UserName}"这样的绑定.这对我没有好处,因为我不知道在设计时字段名称是什么.
我已经编写了一个简化的例子来说明问题.在此示例中,生成包含true和false值的数据表.我的模板中的图像永远不可见.如何根据数据中的真或假值使其不可见?
<Window.Resources>
<!--This is the bit that doesn't work...-->
<Style TargetType="{x:Type Image}" x:Key="HideWhenFalse">
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=???}"
Value="True"> <!--What to put for the path? -->
<Setter Property="Visibility">
<Setter.Value>
Visible
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<!--Up to here-->
<Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Image Source="Images/tick.bmp" Style="{StaticResource HideWhenFalse}">
</Image>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<DataGrid
x:Name="myDataGrid"
AutoGenerateColumns="True" >
</DataGrid>
</Grid>
Run Code Online (Sandbox Code Playgroud)
代码背后:
public partial …