当所选项目发生变化时,如何在longlistselector内显示忙碌指示器?

时间:2015-01-20 09:34:32

标签: wpf xaml c#-4.0 windows-phone-8 windows-phone-8.1

单击longlistselector时,如何在longlistselector中显示繁忙指示符...下面我发布了代码。

它始终显示第一项,但我想显示我点击的项目。

 <StackPanel x:Name="top" Height="54" Orientation="Horizontal" Margin="-13,0,0,0" VerticalAlignment="Top" Background="#FF0FA3C0">
                    <TextBlock TextWrapping="Wrap" Text="{Binding InspectionNumber}" Foreground="#FFF4F4FA" VerticalAlignment="Top" FontSize="26.667" Margin="10,0" Width="200"/>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock TextWrapping="Wrap" Text="{Binding Status}" FontSize="21.333" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,0,15,0" Width="150" FlowDirection="RightToLeft" Foreground="#FFF4F4FA"/>
                        <Border BorderBrush="#CC9D9D9F" BorderThickness="0,0,1,0" HorizontalAlignment="Left" VerticalAlignment="Center" Width="1" Height="30" Margin="0,0,10,0"/>
                        <Grid HorizontalAlignment="Right" Width="40" Height="40" VerticalAlignment="Center">
                            <Image x:Name="sncIcon" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="40" Source="{Binding CloudIcon, Mode=TwoWay,UpdateSourceTrigger=Default}" Tap="CloudIcon_Tap"/>
                            <telerikPrimitives:RadBusyIndicator x:Name="radbusyofflinedownload" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed" Margin="-20,0,0,0" IsRunning="True" AnimationStyle="AnimationStyle8" Foreground="Black" FontSize="24"/>
                        </Grid>
                    </StackPanel>
                </StackPanel>

private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(control);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(control, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is T && fe.Name == ctrlName)
            {
                // Found the control so return
                return child;
            }
            else
            {
                // Not found it - search children
                DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
    }



 private async void InformationListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        RadBusyIndicator radBusyIndicator = FindChildControl<RadBusyIndicator>(this, "radbusyofflinedownload") as RadBusyIndicator;
        radBusyIndicator.Visibility = Visibility.Visible;
    }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

首先,在类

中创建一个属性
 private bool _busyIndicatorIsVisible;

    public bool BusyIndicatorIsVisible
    {
        get 
        { 
            return _busyIndicatorIsVisible; 
        }
        set 
        {
            if (_busyIndicatorIsVisible != value)
            {
                _busyIndicatorIsVisible = value;
                this.OnPropertyChanged("BusyIndicatorIsVisible");
            }
        }
    }

此类实现INotifyPropertyChanged接口。

接下来,在Visibility中使用绑定转换器:

 <telerikPrimitives:RadBusyIndicator x:Name="radbusyofflinedownload" Visibility="{Binding BusyIndicatorIsVisible, Converter={StaticResource BoolVisibilityConverter}}" />

转换器:

 public class BoolVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
         return (bool)value ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

不要忘记设置DataContext。

最后,在InformationListSelector_SelectionChanged

private async void InformationListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    this.BusyIndicatorIsVisible = true;
}

[编辑] 如果RadBusyIndi​​cator在DataTemplate中:

 <telerikPrimitives:RadBusyIndicator x:Name="radbusyofflinedownload" Visibility="{Binding ElementName=NAMELONGLISTSELECTOR,Path=DataContext.BusyIndicatorIsVisible,Converter={StaticResource BoolVisibilityConverter}}" />