如何访问放置在列表框数据模板内的控件?

发布于 2024-08-27 20:53:20 字数 962 浏览 5 评论 0原文

您好,我有以下代码:

<ListBox x:Name="foldersListBox" Grid.Column="0" MouseLeftButtonUp="foldersListBox_MouseLeftButtonUp" 


                             BorderThickness="0"  Height="AUTO" 
                             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                             ScrollViewer.VerticalScrollBarVisibility="Disabled">
                        <DataTemplate>
                            <Border BorderBrush="LightGray"
                            BorderThickness="2"
                            CornerRadius="4">
                                <Image x:Name="folderImage" Width="70" Height="70" Margin="3" />
                            </Border>
                        </DataTemplate>
</ListBox>

现在,当我尝试从后面的代码访问 folderImage 时。我可以使用加载的事件并将发送者类型转换为图像类型,但我不希望那样,因为我想在运行时绑定期间绑定图像源。因此,即使我们尝试加载事件,也不会有所帮助,因为控件不会被加载。

请帮助。

谢谢, 苏本

Hi I have the following code:

<ListBox x:Name="foldersListBox" Grid.Column="0" MouseLeftButtonUp="foldersListBox_MouseLeftButtonUp" 


                             BorderThickness="0"  Height="AUTO" 
                             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                             ScrollViewer.VerticalScrollBarVisibility="Disabled">
                        <DataTemplate>
                            <Border BorderBrush="LightGray"
                            BorderThickness="2"
                            CornerRadius="4">
                                <Image x:Name="folderImage" Width="70" Height="70" Margin="3" />
                            </Border>
                        </DataTemplate>
</ListBox>

Now when I am trying to access folderImage from code behind. I can use the loaded event and typecast the sender as Image type , but I dont want that way,because I want to bind the image source during runtime binding . So even if we will try on loaded event , thatz not going to help as the control wont be loaded .

Help plz.

Thanks,
Subhen

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

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

发布评论

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

评论(1

謸气贵蔟 2024-09-03 20:53:20

你的问题缺少很多细节,但无论如何我都会尽力回答。它与回答您的问题非常不同,但它可能会帮助您了解需要添加问题以指导答案的详细信息。反过来,这个答案可以得到完善。经过一些迭代,您实际上可能会得到答案。

我猜您正在绑定到一组代表“文件夹”的对象,但您希望根据每个对象的状态以编程方式修改所呈现的图像,例如某些FolderType 属性。

如果您的图像来自有限集,解决此问题的方法是使用值转换器。

public class FolderToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Folder folder = value as Folder;
        ImageSource result;
        // Logic to determine which ImageSource to use for a folder.
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

现在看一下这个 XAML:-

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
       <local:FolderToImageConverter x:Key="ImageConverter" />
    </Grid.Resources>
    <ListBox x:Name="foldersListBox">
        <ListBox.ItemTemplate>
             <DataTemplate> 
                <Border BorderBrush="LightGray" BorderThickness="2" CornerRadius="4"> 
                  <Image Source="{Binding Converter={StaticResource ImageConverter}}" Width="70" Height="70" Margin="3" /> 
                </Border> 
             </DataTemplate> 
        </ListBox.ItemTemplate>
    </ListBox> 
</Grid>

将文件夹对象集合绑定到 ListBox ItemsSource 后,它将使用转换器显示一组图像来转换 < code>Folder 对象到正确的 ImageSource 实例。

There is quite a bit of detail missing from your question but I'm going to stab at answering anyway. Its highly unlike to answer your question but it might help you see what detail you need to add the question to guide the answers. In turn this answer can be refined. Some iterations down the road you may actually arrive at an answer.

I'm gonna guess that you are binding to a set of object that represent "Folders" but you want programmatically modify the image being presented depending on the state of each object, for example some FolderType property.

On solution to this is to use a value converter, if your images are from a finite set.

public class FolderToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Folder folder = value as Folder;
        ImageSource result;
        // Logic to determine which ImageSource to use for a folder.
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Now take a look at this XAML:-

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
       <local:FolderToImageConverter x:Key="ImageConverter" />
    </Grid.Resources>
    <ListBox x:Name="foldersListBox">
        <ListBox.ItemTemplate>
             <DataTemplate> 
                <Border BorderBrush="LightGray" BorderThickness="2" CornerRadius="4"> 
                  <Image Source="{Binding Converter={StaticResource ImageConverter}}" Width="70" Height="70" Margin="3" /> 
                </Border> 
             </DataTemplate> 
        </ListBox.ItemTemplate>
    </ListBox> 
</Grid>

Once you have bound your collection of Folder objects to the ListBox ItemsSource it will display a set of images using the converter to transform the Folder object to the correct ImageSource instance.

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