为什么无法操作ControlTemplate?
代码后:
var roomTable = from desks in context.RoomToStandartDesks
where desks.id_room == room.id
select desks.Desk;
var tabItem = new TabItem
{
DataContext = roomTable,
Header = headerText,
};
if (controlTemplate == null)
tabItem.Content = (object)roomTable;
else
tabItem.Content = new ContentControl { Template = controlTemplate };
tabItems.Add(tabItem);
((ContentControl)(tabItem.Content)).Content
== null
((ContentControl)(((ContentControl)(tabItem)).Content)).ContentTemplate
== null
和 xaml ControlTemplate:
<ControlTemplate x:Key="MyTabItemContentTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=x}"/>
<ItemsControl ItemsSource="{Binding DataContext, Converter={StaticResource KeySimplyConvert}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Margin="10" Background="AliceBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<Border.RenderTransform>
<TranslateTransform X="{Binding x, Mode=TwoWay}" Y="{Binding y, Mode=TwoWay}"/>
</Border.RenderTransform>
<Managerer:TablePanel DataContext="{Binding}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ControlTemplate>
为什么? 一使用Silverlight 4。
after code:
var roomTable = from desks in context.RoomToStandartDesks
where desks.id_room == room.id
select desks.Desk;
var tabItem = new TabItem
{
DataContext = roomTable,
Header = headerText,
};
if (controlTemplate == null)
tabItem.Content = (object)roomTable;
else
tabItem.Content = new ContentControl { Template = controlTemplate };
tabItems.Add(tabItem);
((ContentControl)(tabItem.Content)).Content
== null
((ContentControl)(((ContentControl)(tabItem)).Content)).ContentTemplate
== null
and xaml ControlTemplate:
<ControlTemplate x:Key="MyTabItemContentTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=x}"/>
<ItemsControl ItemsSource="{Binding DataContext, Converter={StaticResource KeySimplyConvert}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Margin="10" Background="AliceBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<Border.RenderTransform>
<TranslateTransform X="{Binding x, Mode=TwoWay}" Y="{Binding y, Mode=TwoWay}"/>
</Border.RenderTransform>
<Managerer:TablePanel DataContext="{Binding}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ControlTemplate>
Why?
A use Silverlight 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望我正确理解您的问题...
((ContentControl)(tabItem.Content)).Content
是null
,(( ContentControl)(((ContentControl)(tabItem)).Content)).ContentTemplate
为null
,...在 C# 的第一位之后。我认为这两个问题的答案都与代码中的这一行有关:
我已经简化了此后的括号,希望保持正确性。此外,将
tabItem
转换为ContentControl
以获得相同的Content
属性只会使阅读变得更加困难:(( ContentControl)tabItem.Content).Content
为null
,因为您从未设置新ContentControl
的Content
属性。您只需设置Template
属性。((ContentControl)tabItem.Content).ContentTemplate
为null
的原因几乎相同:您没有设置ContentTemplate
,您设置了模板
。I hope I understand correctly that you're asking...
((ContentControl)(tabItem.Content)).Content
isnull
, and((ContentControl)(((ContentControl)(tabItem)).Content)).ContentTemplate
isnull
,... after the first bit of C#. Answers to both I think have to do with this line from your code:
I've simplified the parantheses hereafter, hopefully maintaining correctness. Also, casting
tabItem
toContentControl
to get at the sameContent
property just makes it harder to read:((ContentControl)tabItem.Content).Content
isnull
because you never set theContent
property of the newContentControl
. You only set theTemplate
property.((ContentControl)tabItem.Content).ContentTemplate
isnull
for pretty much the same reason: You didn't set theContentTemplate
, you set theTemplate
.