用作数据模板时用户控件未呈现?

发布于 2024-08-09 01:04:10 字数 6431 浏览 2 评论 0 原文

我有一个用户控件,我想将其用作列表框中的数据模板。

这是可行的:

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
<Grid x:Name="Grid" Height="100" Width="880" Background="LightGray">
    <Grid.RowDefinitions>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="190" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="190" />
        <ColumnDefinition Width="200" />
    </Grid.ColumnDefinitions>
    <Label Grid.Column="0" Grid.Row="0">Client</Label>
    <Label Grid.Column="0" Grid.Row="2">Contact</Label>
    <Label Grid.Column="1" Grid.Row="0">Date Presentation</Label>
    <Label Grid.Column="2" Grid.Row="0">Action</Label>
    <Label Grid.Column="3" Grid.Row="0">Date Interview</Label>
    <Label Grid.Column="3" Grid.Row="2">Time Interview</Label>
    <Label Grid.Column="4" Grid.Row="0">Remarks</Label>
    <Label Grid.Column="5" Margin="0,0,2,0">managed by</Label>
    <ComboBox Grid.Column="0" Grid.Row="1" Margin="2" Text="{Binding Path=Customer}">
        <!--Template-->
    </ComboBox>
    <TextBox Grid.Column="0" Grid.Row="3" Margin="2" Text="{Binding Path=Contact}"></TextBox>
    <TextBox Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Path=PresentationDate}"></TextBox>
    <ComboBox Grid.Column="2" Grid.Row="1" Margin="2" Text="{Binding Path=Action}">
        <!--Template-->
    </ComboBox>
    <TextBox Grid.Column="3" Grid.Row="1" Margin="2" Text="{Binding Path=InterviewDate}"></TextBox>
    <TextBox Grid.Column="3" Grid.Row="3" Margin="2" Text="{Binding Path=InterviewTime}"></TextBox>
    <TextBox Grid.Column="4" Grid.Row="1" Grid.RowSpan="3" Margin="2" Text="{Binding Path=Remarks}"></TextBox>
    <StackPanel Orientation="Horizontal" Grid.Column="5" Grid.Row="1" >
        <ComboBox Width="124" Text="{Binding Path=Manager}" Margin="2"></ComboBox>
        <Button Width="60" Height="20" Margin="4,0,0,0" >Mail</Button>
    </StackPanel>
    <CheckBox Grid.Column="5" Grid.Row="3" Margin="2,2,4,2">Rejection communicated</CheckBox>
   </Grid>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果我在 > 之间放置完全相同的代码标签:

<UserControl x:Class="CandiMan.View.CandidatePresentationControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cm="clr-namespace:CandiMan;assembly=CandiMan"  
    xmlns:vw="clr-namespace:CandiMan.View;assembly=CandiMan"
    xmlns:vm="clr-namespace:CandiMan.ViewModel;assembly=CandiMan"             
    Height="100" Width="880" BorderBrush="Black" BorderThickness="1">

    <Grid x:Name="Grid" Height="100" Width="880" Background="LightGray">
        <Grid.RowDefinitions>
            <RowDefinition Height="24"/>
            <RowDefinition Height="24"/>
            <RowDefinition Height="24"/>
            <RowDefinition Height="24"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="190" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="190" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0" Grid.Row="0">Client</Label>
        <Label Grid.Column="0" Grid.Row="2">Contact</Label>
        <Label Grid.Column="1" Grid.Row="0">Date Presentation</Label>
        <Label Grid.Column="2" Grid.Row="0">Action</Label>
        <Label Grid.Column="3" Grid.Row="0">Date Interview</Label>
        <Label Grid.Column="3" Grid.Row="2">Time Interview</Label>
        <Label Grid.Column="4" Grid.Row="0">Remarks</Label>
        <Label Grid.Column="5" Margin="0,0,2,0">managed by</Label>
        <ComboBox Grid.Column="0" Grid.Row="1" Margin="2" Text="{Binding Path=Customer}">
            <!--Template-->
        </ComboBox>
        <TextBox Grid.Column="0" Grid.Row="3" Margin="2" Text="{Binding Path=Contact}"></TextBox>
        <TextBox Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Path=PresentationDate}"></TextBox>
        <ComboBox Grid.Column="2" Grid.Row="1" Margin="2" Text="{Binding Path=Action}">
            <!--Template-->
        </ComboBox>
        <TextBox Grid.Column="3" Grid.Row="1" Margin="2" Text="{Binding Path=InterviewDate}"></TextBox>
        <TextBox Grid.Column="3" Grid.Row="3" Margin="2" Text="{Binding Path=InterviewTime}"></TextBox>
        <TextBox Grid.Column="4" Grid.Row="1" Grid.RowSpan="3" Margin="2" Text="{Binding Path=Remarks}"></TextBox>
        <StackPanel Orientation="Horizontal" Grid.Column="5" Grid.Row="1" >
            <ComboBox Width="124" Text="{Binding Path=Manager}" Margin="2"></ComboBox>
            <Button Width="60" Height="20" Margin="4,0,0,0" >Mail</Button>
        </StackPanel>
        <CheckBox Grid.Column="5" Grid.Row="3" Margin="2,2,4,2">Rejection communicated</CheckBox>
       </Grid>

</UserControl>

进行操作

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <vw:CandidatePresentationControl/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

进入名为 CandidatePresentationControl 的用户控件,并像未渲染一样 。没有错误,只是一个空列表框。有人可以帮助我吗?

谢谢你!

编辑:我忘记了一些东西,不知道它是否重要:我所做的整个事情也是一个用户控件。

I have a usercontrol which I want to use as a DataTemplate in a Listbox.

This works:

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
<Grid x:Name="Grid" Height="100" Width="880" Background="LightGray">
    <Grid.RowDefinitions>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="190" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="190" />
        <ColumnDefinition Width="200" />
    </Grid.ColumnDefinitions>
    <Label Grid.Column="0" Grid.Row="0">Client</Label>
    <Label Grid.Column="0" Grid.Row="2">Contact</Label>
    <Label Grid.Column="1" Grid.Row="0">Date Presentation</Label>
    <Label Grid.Column="2" Grid.Row="0">Action</Label>
    <Label Grid.Column="3" Grid.Row="0">Date Interview</Label>
    <Label Grid.Column="3" Grid.Row="2">Time Interview</Label>
    <Label Grid.Column="4" Grid.Row="0">Remarks</Label>
    <Label Grid.Column="5" Margin="0,0,2,0">managed by</Label>
    <ComboBox Grid.Column="0" Grid.Row="1" Margin="2" Text="{Binding Path=Customer}">
        <!--Template-->
    </ComboBox>
    <TextBox Grid.Column="0" Grid.Row="3" Margin="2" Text="{Binding Path=Contact}"></TextBox>
    <TextBox Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Path=PresentationDate}"></TextBox>
    <ComboBox Grid.Column="2" Grid.Row="1" Margin="2" Text="{Binding Path=Action}">
        <!--Template-->
    </ComboBox>
    <TextBox Grid.Column="3" Grid.Row="1" Margin="2" Text="{Binding Path=InterviewDate}"></TextBox>
    <TextBox Grid.Column="3" Grid.Row="3" Margin="2" Text="{Binding Path=InterviewTime}"></TextBox>
    <TextBox Grid.Column="4" Grid.Row="1" Grid.RowSpan="3" Margin="2" Text="{Binding Path=Remarks}"></TextBox>
    <StackPanel Orientation="Horizontal" Grid.Column="5" Grid.Row="1" >
        <ComboBox Width="124" Text="{Binding Path=Manager}" Margin="2"></ComboBox>
        <Button Width="60" Height="20" Margin="4,0,0,0" >Mail</Button>
    </StackPanel>
    <CheckBox Grid.Column="5" Grid.Row="3" Margin="2,2,4,2">Rejection communicated</CheckBox>
   </Grid>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

If I put the exact same code from between the <DataTemplate> tags:

<UserControl x:Class="CandiMan.View.CandidatePresentationControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cm="clr-namespace:CandiMan;assembly=CandiMan"  
    xmlns:vw="clr-namespace:CandiMan.View;assembly=CandiMan"
    xmlns:vm="clr-namespace:CandiMan.ViewModel;assembly=CandiMan"             
    Height="100" Width="880" BorderBrush="Black" BorderThickness="1">

    <Grid x:Name="Grid" Height="100" Width="880" Background="LightGray">
        <Grid.RowDefinitions>
            <RowDefinition Height="24"/>
            <RowDefinition Height="24"/>
            <RowDefinition Height="24"/>
            <RowDefinition Height="24"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="190" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="190" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0" Grid.Row="0">Client</Label>
        <Label Grid.Column="0" Grid.Row="2">Contact</Label>
        <Label Grid.Column="1" Grid.Row="0">Date Presentation</Label>
        <Label Grid.Column="2" Grid.Row="0">Action</Label>
        <Label Grid.Column="3" Grid.Row="0">Date Interview</Label>
        <Label Grid.Column="3" Grid.Row="2">Time Interview</Label>
        <Label Grid.Column="4" Grid.Row="0">Remarks</Label>
        <Label Grid.Column="5" Margin="0,0,2,0">managed by</Label>
        <ComboBox Grid.Column="0" Grid.Row="1" Margin="2" Text="{Binding Path=Customer}">
            <!--Template-->
        </ComboBox>
        <TextBox Grid.Column="0" Grid.Row="3" Margin="2" Text="{Binding Path=Contact}"></TextBox>
        <TextBox Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Path=PresentationDate}"></TextBox>
        <ComboBox Grid.Column="2" Grid.Row="1" Margin="2" Text="{Binding Path=Action}">
            <!--Template-->
        </ComboBox>
        <TextBox Grid.Column="3" Grid.Row="1" Margin="2" Text="{Binding Path=InterviewDate}"></TextBox>
        <TextBox Grid.Column="3" Grid.Row="3" Margin="2" Text="{Binding Path=InterviewTime}"></TextBox>
        <TextBox Grid.Column="4" Grid.Row="1" Grid.RowSpan="3" Margin="2" Text="{Binding Path=Remarks}"></TextBox>
        <StackPanel Orientation="Horizontal" Grid.Column="5" Grid.Row="1" >
            <ComboBox Width="124" Text="{Binding Path=Manager}" Margin="2"></ComboBox>
            <Button Width="60" Height="20" Margin="4,0,0,0" >Mail</Button>
        </StackPanel>
        <CheckBox Grid.Column="5" Grid.Row="3" Margin="2,2,4,2">Rejection communicated</CheckBox>
       </Grid>

</UserControl>

into a usercontrol named CandidatePresentationControl and do it like

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <vw:CandidatePresentationControl/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

it does not get rendered. No errors, just an empty listbox. Can someone help me??

Thank you!

edit: I forgot something, dunno if it matters: The whole thing I'm doing this in, is a usercontrol, too.

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

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

发布评论

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

评论(2

耶耶耶 2024-08-16 01:04:10

您引用的 UserControl 位于另一个 UserControl 中并不重要。尝试以下步骤以更好地调试 XAML 代码: http://beacosta.com/blog/?p =52

由于您的数据是在 XAML 中硬连接的,所以解释空 ListBox 的唯一方法是,您的 UserControl 无法被父 UserControl 找到,imo。

It shouldn't matter, that your referenced UserControl is within another UserControl. Try these steps to better debug your XAML-code: http://beacosta.com/blog/?p=52

Since you have your data hard wired in XAML, the only way to explain the empty ListBox is, that your UserControl can't be found by the parent UserControl, imo.

尸血腥色 2024-08-16 01:04:10
<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <vw:CandidatePresentationControl DataContext="{Binding}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

你必须这样写才能绑定数据上下文,我建议你看看 MVVM,它会让你知道如何更好地做到这一点。

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <vw:CandidatePresentationControl DataContext="{Binding}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

You have to write this way in order to bind datacontext, I would suggest you look at MVVM that will give you idea on how to do it even better way.

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