控件作为Grid的背景

发布于 2024-12-09 22:38:32 字数 983 浏览 1 评论 0原文

我有一个网格,我需要一个控件作为该网格的背景。该控件将是一个进度条,但如何创建它是我的问题。

我找不到如何将控件设置为网格背景。

您有解决此类问题的经验吗?

    <DataTemplate x:Key="TodoItemWeeklyTemplate">
        <Grid MinWidth="800" Background="Transparent">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Image Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Occurrence.Appointment.Icon, Converter={StaticResource imgConverter}}" Width="16" Height="16" Stretch="Fill" />
            <TextBlock Grid.Column="1" HorizontalAlignment="Left" MaxWidth="130" Text="{Binding Occurrence.Appointment.Subject}" />

        </Grid>
    </DataTemplate>

I have a grid, and I need to have a control as background of this grid. This control will be a progressbar, but it's my problem how to create it.

I can't find how can I set a control as background of grid.

Have you got any experience with this kind of problem?

    <DataTemplate x:Key="TodoItemWeeklyTemplate">
        <Grid MinWidth="800" Background="Transparent">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Image Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Occurrence.Appointment.Icon, Converter={StaticResource imgConverter}}" Width="16" Height="16" Stretch="Fill" />
            <TextBlock Grid.Column="1" HorizontalAlignment="Left" MaxWidth="130" Text="{Binding Occurrence.Appointment.Subject}" />

        </Grid>
    </DataTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

反差帅 2024-12-16 22:38:32

以下是我的做法:-

    <Grid MinWidth="800">
        <Rectangle x:Name="Background" Fill="Green" Width="{Binding PercentDone, Converter={StaticConverter WidthConv}, ConverterParameter=800}" HorizontalAlignment="Left" />
        <Grid Background="Transparent"> 

            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="Auto" /> 
                <ColumnDefinition Width="*" /> 
            </Grid.ColumnDefinitions> 

            <Grid.RowDefinitions> 
                <RowDefinition Height="*" /> 
            </Grid.RowDefinitions> 

            <Image Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Occurrence.Appointment.Icon, Converter={StaticResource imgConverter}}" Width="16" Height="16" Stretch="Fill" /> 
            <TextBlock Grid.Column="1" HorizontalAlignment="Left" MaxWidth="130" Text="{Binding Occurrence.Appointment.Subject}" /> 

        </Grid>
    </Grid>

其中 WidthConv 是添加到实现 IValueConverter 的用户控件资源中的类的实例。 Convert 方法将获取输入的百分比值并将其应用于参数以返回表示该百分比值的矩形所需的宽度。

这里重要的一点是,当 Grid 占据相同区域时,元素自然会相互覆盖。

Here is how I would do it:-

    <Grid MinWidth="800">
        <Rectangle x:Name="Background" Fill="Green" Width="{Binding PercentDone, Converter={StaticConverter WidthConv}, ConverterParameter=800}" HorizontalAlignment="Left" />
        <Grid Background="Transparent"> 

            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="Auto" /> 
                <ColumnDefinition Width="*" /> 
            </Grid.ColumnDefinitions> 

            <Grid.RowDefinitions> 
                <RowDefinition Height="*" /> 
            </Grid.RowDefinitions> 

            <Image Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Occurrence.Appointment.Icon, Converter={StaticResource imgConverter}}" Width="16" Height="16" Stretch="Fill" /> 
            <TextBlock Grid.Column="1" HorizontalAlignment="Left" MaxWidth="130" Text="{Binding Occurrence.Appointment.Subject}" /> 

        </Grid>
    </Grid>

Where WidthConv is an instance of a class added to the usercontrol resources that implements IValueConverter. The Convert method would take the input percentage value and apply it to the parameter to return the width the rectangle needs to be to represents that percentage value.

The important point here is that Grid naturaly overlays elements on each other when the occupy the same area.

∞琼窗梦回ˉ 2024-12-16 22:38:32

您无法将控件设置为背景,因为它是 Brush 类型。

但如果你想模仿,你可以将你的网格放入另一个网格中,如下所示:

<Grid>

   <Grid>
      <!-- put your Content here -->
   </Grid>

   <ProgressBar />

</Grid>

You cannot set a control as a background as it is of Brush Type.

But if you want to emulate that you can put your Grid in another one like that :

<Grid>

   <Grid>
      <!-- put your Content here -->
   </Grid>

   <ProgressBar />

</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文