数据绑定问题请教~~~~~送分贴

wqbyangtzeu 2013-08-22 02:41:33
本人初学XAML,在学习过程中碰到如下问题:
XAML文件中有一个listBox控件,这个控件中的每个item是一个textbox和Image的组合,现在这个控件的每个Item中的Image要根据我后台的一个bool变量值变化来自动显示不同的图片,true显示图片1,false 显示图片2.
问题如下:
1.如果在后台处理该如何找到Image控件,然后通过重新指定路径来更换图片
2.可以在前台处理用模板或者什么来处理这个问题么?毕竟我图片就2种情况,最好不用后台来处理,直接前台自动根据bool值的变化来自动更换图片
...全文
256 4 打赏 收藏 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qlx647625 2013-12-06
  • 打赏
  • 举报
回复
刚才没认真看帖子,2楼用的就是转换类 其实可以在xaml里面加所谓的条件style实现,感觉复杂程度比写转换类低
qlx647625 2013-12-06
  • 打赏
  • 举报
回复
绑定时候 指定转换类型和属性, 后台写个转换类,或者使用上面的方法都可以,要看复杂程度和数据量 比较简单的切下图片或者样式什么的就不用写类了,复杂的使用转换类的办法效率比较高
Bonjour-你好 2013-08-22
  • 打赏
  • 举报
回复
引用
如果在后台处理该如何找到Image控件

在XAML中,向 Image 控件添加 Name 属性,后台就可以引用了。

不知道你的bool变量是具体到绑定数据到 ListBox 的数据中,还是是整个用户控件(或窗口等)的一个字段,暂且认为是第一种情况,写个例子,你可以慢慢参考:

<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="Window1" Height="300" Width="300">

<Window.Resources>
<local:BoolToPictureConverter x:Key="boolToPic"/>

<DataTemplate x:Key="myListItemViewTemplate">
<Grid Margin="2">
<StackPanel Orientation="Horizontal">
<Image Height="60" Stretch="Fill" Source="{Binding IsWhat, Converter={StaticResource boolToPic}}"/>
<TextBox FontSize="15" Margin="5,0,0,0" FontWeight="Bold" Text="{Binding SomeThing}"/>
</StackPanel>
</Grid>
</DataTemplate>
</Window.Resources>

<Grid>
<ListBox Height="201" HorizontalAlignment="Left" Margin="29,29,0,0" Name="listBox1" VerticalAlignment="Top" Width="214" ItemTemplate="{StaticResource myListItemViewTemplate}" />
</Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media.Imaging;

namespace WpfTest
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

List<TestClass> list = new List<TestClass>();
list.Add(new TestClass { IsWhat = true, SomeThing = "真" });
list.Add(new TestClass { IsWhat = false, SomeThing = "假" });
list.Add(new TestClass { IsWhat = false, SomeThing = "假" });
list.Add(new TestClass { IsWhat = true, SomeThing = "真" });

listBox1.ItemsSource = list;
}
}

public class TestClass
{
public Boolean IsWhat { get; set; }
public String SomeThing { get; set; }
}

//转换器
public class BoolToPictureConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Boolean isBool = (Boolean)value;
if (isBool) {
return new BitmapImage(new Uri(@"/Images/Bluehills.jpg", UriKind.RelativeOrAbsolute));
}

return new BitmapImage(new Uri(@"/Images/Sunset.jpg", UriKind.RelativeOrAbsolute));
}

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

}


Architecture Net 2013-08-22
  • 打赏
  • 举报
回复
使用MVVM模式,在前台绑定数据的时候,增加一个URL判断属性A,此属性根据后台BOOL类型属性B,选择不同的URL地址,然后前台绑定URL判断属性A即可。

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧