【问题标题】:BusyIndicator.Message Binding to Collection of stringsBusyIndi​​cator.Message 绑定到字符串集合
【发布时间】:2011-01-03 00:15:15
【问题描述】:

我正在尝试将 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");
        }
    }

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

谢谢,
标记

【问题讨论】:

  • Visual Studio 是否在抱怨输出窗口在运行时出现绑定错误?
  • 输出窗口中没有绑定错误。很好的建议,无论如何感谢您的及时回复。仍然在这个问题上抨击我!

标签: silverlight-3.0 toolkit busyindicator


【解决方案1】:

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

<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" ) );
        }
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2021-11-09
    相关资源
    最近更新 更多