如何访问放置在列表框数据模板内的控件?
您好,我有以下代码:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题缺少很多细节,但无论如何我都会尽力回答。它与回答您的问题非常不同,但它可能会帮助您了解需要添加问题以指导答案的详细信息。反过来,这个答案可以得到完善。经过一些迭代,您实际上可能会得到答案。
我猜您正在绑定到一组代表“文件夹”的对象,但您希望根据每个对象的状态以编程方式修改所呈现的图像,例如某些FolderType 属性。
如果您的图像来自有限集,解决此问题的方法是使用值转换器。
现在看一下这个 XAML:-
将文件夹对象集合绑定到
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.
Now take a look at this XAML:-
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 theFolder
object to the correctImageSource
instance.