English 中文(简体)
• 如何在Grid进行集团控制,以便在XAML进行再利用?
原标题:How to group controls inside Grid for reuse in XAML?
  • 时间:2010-09-24 07:03:11
  •  标签:
  • c#
  • wpf
  • xaml

在我的申请中,我有一些控制,这些控制自然属于一起,在不同窗口中重新使用许多地方。 这些控制总是放在网内。

我不想在我每次使用时复制控制(和后面的编码),而是将控制定义为单一用户“Control”。

我现在谈谈:

<Grid>
    <Grid.ColumnDefinitions>
        [ColumnDefinitions...]
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        [RowDefinitions...]
    </Grid.RowDefinitions>

    <StackPanel Grid.Column="0" Grid.Row="0">
        <TextBlock Text="Caption" />
        <Border Padding="2" x:Name="myBorder">
            <TextBox TabIndex="1" x:Name="myTxt"/>
        </Border>
    </StackPanel>
    <ListBox x:Name="myList" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Margin="5,50,5,0" Height="100" VerticalAlignment="Top" Visibility="Collapsed" />

    [More controls..]
</Grid>

但我要重复这一部分:

    <StackPanel Grid.Column="0" Grid.Row="0">
        <TextBlock Text="Caption" />
        <Border Padding="2" x:Name="myBorder">
            <TextBox TabIndex="1" x:Name="myTxt"/>
        </Border>
    </StackPanel>
    <ListBox x:Name="myList" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Margin="5,50,5,0" Height="100" VerticalAlignment="Top" Visibility="Collapsed" />

作为单一控制,但我如何界定Grid。 使用控制时的颜色(有时作为参数提供)? - 我如何确定Grid。 RowSpan数值(尽管该代码已移至新的xaml文档,但未在电网内界定)?

任何评论?

问题回答

将它们单独置于用户控制之下,然后在您的项目中纳入这一点。

如果你重新使用Blend,它就非常容易,只是选择了所有控制,正确点击和让用户控制。

You could also make it into a resource. Define it in a ResourceDictionary and include the dictionary the places you want to use it. There is one catch - the resource dictionary returns the same instance everytime - so you have to add the attribute x:Shared="false".

但是,要看一下你如何用数据图表来做到这一点:





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签