0

我正在尝试将 SL BusyIndi​​cator 绑定到繁忙消息的集合。当集合有项目时,指示器将显示消息。当消息集合为空时,指示器将隐藏。

首先,指示器没有显示我的消息,我看到的只是一个空白指示器框,带有一个不确定的进度条:

<UserControl.Resources>

...
<anotherAssembly:CollectionToBoolConverter x:Key="CollectionToBoolConverter" />

<DataTemplate x:Key="LoadingMessageDataTemplate">
    <ItemsControl x:Name="itemsControl" ItemsSource="{Binding AllocationLoadingMessages}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</DataTemplate>

...

</UserControl.Resources>

...

<controlToolkit:BusyIndicator 
    IsBusy="{Binding AllocationLoadingMessages, Converter={StaticResource CollectionToBoolConverter}}"
    BusyContent="{Binding AllocationLoadingMessages}"
    BusyContentTemplate="{StaticResource LoadingMessageDataTemplate}"/>
///content
</controlToolkit:BusyIndicator>

...

视图模型:

    private ObservableCollection<string> _allocationLoadingMessages = new ObservableCollection<string>();
    public ObservableCollection<string> AllocationLoadingMessages
    {
        get { return _allocationLoadingMessages; }
        set
        {
            SetValue(ref _allocationLoadingMessages, value, "AllocationLoadingMessages");
        }
    }

那么如何在我的指标中获得一个简单的消息列表呢?

谢谢,
马克

4

1 回答 1

0

您的代码非常接近目标,您需要做的就是替换该行

<ItemsControl x:Name="itemsControl" ItemsSource="{Binding AllocationLoadingMessages}" >

<ItemsControl x:Name="itemsControl" ItemsSource="{Binding}" >

我在 Silverlight 3 和 5 中都对此进行了测试,所以我认为它也可以在 4 中使用。

我不确定您set { }的房产代码AllocationLoadingMessages是干什么用的;您不必在每次更改集合时都替换它。当我让它工作时,我在视图模型中所做的只是:

    public ObservableCollection<string> AllocationLoadingMessages { get; set; }

    private int _loadingMessageNumber = 0;
    private void Add()
    {
        AllocationLoadingMessages.Add( "Loading message #" + ++_loadingMessageNumber );
        PropertyChanged( this, new PropertyChangedEventArgs( "AllocationLoadingMessages" ) );
    }

    private void RemoveFirst()
    {
        if( AllocationLoadingMessages.Count > 0 )
        {
            AllocationLoadingMessages.RemoveAt( 0 );
            PropertyChanged( this, new PropertyChangedEventArgs( "AllocationLoadingMessages" ) );
        }
    }

很抱歉我迟到了(直到现在才遇到这个问题),但我希望它至少可以帮助其他正在寻找答案的人。

于 2012-04-11T08:46:31.607 回答