为什么无法操作ControlTemplate?

发布于 2024-11-15 10:26:38 字数 1754 浏览 4 评论 0原文

代码后:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你 2024-11-22 10:26:38

我希望我正确理解您的问题...

  1. 为什么 ((ContentControl)(tabItem.Content)).Contentnull
  2. 为什么 (( ContentControl)(((ContentControl)(tabItem)).Content)).ContentTemplatenull

...在 C# 的第一位之后。我认为这两个问题的答案都与代码中的这一行有关:

else
    tabItem.Content = new ContentControl { Template = controlTemplate };

我已经简化了此后的括号,希望保持正确性。此外,将 tabItem 转换为 ContentControl 以获得相同的 Content 属性只会使阅读变得更加困难:

  1. (( ContentControl)tabItem.Content).Contentnull,因为您从未设置新 ContentControlContent 属性。您只需设置 Template 属性。
  2. ((ContentControl)tabItem.Content).ContentTemplatenull 的原因几乎相同:您没有设置 ContentTemplate,您设置了模板

I hope I understand correctly that you're asking...

  1. Why ((ContentControl)(tabItem.Content)).Content is null, and
  2. Why ((ContentControl)(((ContentControl)(tabItem)).Content)).ContentTemplate is null,

... after the first bit of C#. Answers to both I think have to do with this line from your code:

else
    tabItem.Content = new ContentControl { Template = controlTemplate };

I've simplified the parantheses hereafter, hopefully maintaining correctness. Also, casting tabItem to ContentControl to get at the same Content property just makes it harder to read:

  1. ((ContentControl)tabItem.Content).Content is null because you never set the Content property of the new ContentControl. You only set the Template property.
  2. ((ContentControl)tabItem.Content).ContentTemplate is null for pretty much the same reason: You didn't set the ContentTemplate, you set the Template.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文