我有一个数据网格控件模板。 İt里面有一个名为 LoadingAnimation 的用户控件:
<ControlTemplate x:Key="TemplateDataGridPrintAndExport" TargetType="{x:Type DataGrid}">
<Grid Margin="5">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<Label Content="{TemplateBinding ToolTip}" ></Label>
</Border>
<Border Margin="0,30,0,0" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,0,1,1" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="False">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Command="ApplicationCommands.SelectAll" Focusable="False" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}">
<Button.Visibility>
<Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
<Binding.ConverterParameter>
<DataGridHeadersVisibility>All</DataGridHeadersVisibility>
</Binding.ConverterParameter>
</Binding>
</Button.Visibility>
</Button>
<Grid Grid.Column="1" Grid.Row="2" HorizontalAlignment="Right">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Style="{StaticResource StyleButtons}" Command="ApplicationCommands.NotACommand" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="../Styles/icons/page_excel.png" Grid.Column="0"/>
<TextBlock Text="Excel" Grid.Column="1" />
<UserControls:LoadingAnimation HorizontalAlignment="Center" x:Name="btnCustomWaitForExcel" Grid.Column="2" />
</Grid>
</Button>
<Button Grid.Column="1" Style="{StaticResource StyleButtons}" Command="ApplicationCommands.Print" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="../Styles/icons/printer.png" Grid.Column="0"/>
<TextBlock Text="Yazdır" Grid.Column="1" />
</Grid>
</Button>
</Grid>
<DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
<DataGridColumnHeadersPresenter.Visibility>
<Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
<Binding.ConverterParameter>
<DataGridHeadersVisibility>Column</DataGridHeadersVisibility>
</Binding.ConverterParameter>
</Binding>
</DataGridColumnHeadersPresenter.Visibility>
</DataGridColumnHeadersPresenter>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" Grid.ColumnSpan="2" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Grid.Row="1"/>
<ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
<Grid Grid.Column="1" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
将 ApplicationCommands.NotACommand 绑定到datagrid底部的按钮(名为btnCustomWaitForExcel)。
ApplicationCommands.NotACommand 绑定到一个函数:
CommandBindings.Add(new CommandBinding(ApplicationCommands.NotACommand, ExportExecuted));
在ExportExecuted方法中,我尝试了四种不同的方法来访问datagrid模板中的 btnCustomWaitForExcel :
private async void ExportExecuted(object sender, ExecutedRoutedEventArgs e)
{
try
{
//1. method
((LoadingAnimation)(LogicalTreeHelper.FindLogicalNode((DependencyObject)LogicalTreeHelper.GetChildren(((DataGrid)e.Parameter)), "btnCustomWaitForExcel"))).Visibility = Visibility.Hidden;
//2. method
((LoadingAnimation)(((DataGrid)e.Parameter).FindName("btnCustomWaitForExcel"))).Visibility = Visibility.Visible;
//3. method
bool isfind = false;
DependencyObject parent = ((DataGrid)e.Parameter);
while (!isfind)
{
IEnumerable children = LogicalTreeHelper.GetChildren(parent);
foreach (object child in children)
{
if (child is DependencyObject)
{
DependencyObject depChild = child as DependencyObject;
if (depChild.GetValue(FrameworkElement.NameProperty).ToString() == "btnCustomWaitForExcel")
{
isfind = true;
depChild.SetValue(FrameworkElement.VisibilityProperty, Visibility.Hidden);
}
parent = child as DependencyObject;
}
}
}
//4. method
((LoadingAnimation)((DataGrid)e.Parameter).Template.FindName("btnCustomWaitForExcel", dgrdYonetimRolYetkiRol)).Visibility = Visibility.Hidden;
//Excell esportation
var task = Task.Factory.StartNew(() => ExportExecuted_async(sender, e));
await task;
}
catch (Exception ex)
{
globalTool.insertError(ex, globalUser.ID, true);
}
}
我无法更改用户控件的可见性。有什么想法来改变数据网格控件模板中用户控件的可见性吗?
答案 0 :(得分:1)
您的第二种方法已关闭,但您不必在控件上使用FindName()
方法,而是必须在ControlTemplate
上使用它。在这种情况下,您实际上有2个嵌套ControlTemplate
。首先,您必须在ScrollViewer
的模板中找到DataGrid
。接下来,您将在ScrollViewer
:
LoadingAnimation loadingAnim = null;
var viewer = ((DataGrid)e.Parameter).Template.FindName("DG_ScrollViewer")
as ScrollViewer;
if(viewer != null){
loadingAnim = viewer.Template.FindName("btnCustomWaitForExcel")
as LoadingAnimation;
if(loadingAnim != null) loadingAnim.Visibility = Visibility.Visible;
}
根据安全指南添加if
条件,通常它们都应该通过并执行内部代码。
答案 1 :(得分:0)
您可以简单地将按钮的标签绑定到btnCustomWaitForExcel
用户控件本身吗?然后,在命令执行中,将源代码转换为按钮,然后将其标记为LoadingAnimation控件,并更改可见性。