将ResourceDictionary控件绑定到多个对象

时间:2016-08-09 19:36:48

标签: c# wpf

目前在我的资源词典中我有一个telerik Tile控件。当它第二次实例化时,我收到以下错误Specified Visual is already a child of another Visual or the root of a CompositionTarget

public void ListIcon()
{
    var tile = Application.Current.Resources["ListTile"] as Tile;        
    Tiles.Add(tile);
}

的App.xaml

<telerik:Tile x:Key="Calendar" TileType="Double" Background="#FF79256B" Group="Other">
    <telerik:RadCalendar x:Name="calendar" 
                         FontFamily="Segoe UI"
                         FontSize="10"
                         controls:StyleManager.Theme="Windows8"
                         VerticalAlignment="Top" HorizontalAlignment="Center"
                         SelectionMode="Extended" 
                         IsTodayHighlighted="True" ViewsHeaderVisibility="Collapsed"                                     
                         SelectedDate="{Binding SelectedDate, Mode=TwoWay}"
                         Culture="en-US"
                         DayTemplateSelector="{StaticResource EventDayTemplateSelector}"/>
</controls:Tile>

每次登录新用户时,都会调用ListIcon()。如果我注销并登录到第二个用户,则会发生错误。请问如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

只需设置控件的x:Shared="false"就可以了。

<telerik:Tile x:Shared="False" x:Key="Calendar" TileType="Double" Background="#FF79256B" Group="Other">
    <telerik:RadCalendar x:Name="calendar" 
                         FontFamily="Segoe UI"
                         FontSize="10"
                         controls:StyleManager.Theme="Windows8"
                         VerticalAlignment="Top" HorizontalAlignment="Center"
                         SelectionMode="Extended" 
                         IsTodayHighlighted="True" ViewsHeaderVisibility="Collapsed"                                     
                         SelectedDate="{Binding SelectedDate, Mode=TwoWay}"
                         Culture="en-US"
                         DayTemplateSelector="{StaticResource EventDayTemplateSelector}"/>
</controls:Tile>