我正在使用SilverLight数据网格显示用户信息,我有一个用于打开弹出窗口的链接,目前这个按钮显示所有记录,我想只显示年龄> 18 如何隐藏某些记录的按钮? 这是我的网格
<sdk:DataGrid x:Name="myDG" ItemsSource="{Binding Pages}"
AutoGenerateColumns="False" HeadersVisibility="All" Grid.Row="2" Grid.ColumnSpan="2"
IsReadOnly="True" CanUserResizeColumns="True" Margin="0" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="ID" MinWidth="100" Binding="{Binding Id}" />
<sdk:DataGridTextColumn Header="Name" MinWidth="100" Binding="{Binding Name}" />
<sdk:DataGridTextColumn Header="Number" MinWidth="100" Binding="{Binding Number}" Width="0.3*" />
<sdk:DataGridTextColumn Header="Gender" MinWidth="100" Binding="{Binding Gender}" Width="0.3*" />
<sdk:DataGridTextColumn Header="Age" MinWidth="100" Binding="{Binding Age}" Width="0.3*" />
<sdk:DataGridTextColumn Header="CREATE DATE" MinWidth="100" Binding="{Binding A}" Width="0.3*" />
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<HyperlinkButton x:Name="Type" Content="Hire" Margin="5,5,0,0" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="Hire" cal:View.Model="{Binding Source={StaticResource VMResource}}">
<cal:Parameter Value="{Binding }" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</HyperlinkButton>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
我尝试在我的DataSource中添加一个变量名CanHire并指定这样,但它仍然可见所有记录
<HyperlinkButton Visibility="Binding CanHire">
答案 0 :(得分:1)
你的属性CanHire是bool但是可见性不是,你不能像那样绑定(并且你的绑定中缺少花括号)。 试试这个:
<HyperlinkButton IsEnabled="{Binding CanHire}"/>
或者:
<someControl.Resources>
<BoolToVisibilityConverter x:Key="VisibleIfTrue"/>
</someControl.Resources>
...
<HyperlinkButton Visibility="{Binding CanHire,
Converter={StaticResource VisibleIfTrue}}"/>
您必须自己实施转换器或使用第三方工具包中的转换器:
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert( object value, Type targetType,
object parameter, CultureInfo culture )
{
return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
}
}