WPF中如何把listBox选中的对象绑定在图片的Source上
对象的定义如下:classimg{publicStringName{get;set;}publicStringURL{get;set;}publicimg(Stringn...
对象的定义如下:
class img
{
public String Name { get; set; }
public String URL { get; set; }
public img(String name,String url) {
this.Name = name;
this.URL = url;
}
}
XAML代码:
<Grid>
<Button Content="Clear" ">
<ListBox Name="listBox" />
<Image x:Name="img" Source="Assets/img/Alpaca.jpg" />
<TextBlock x:Name="tb" HorizontalAlignment="Left" Margin="276,280,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding SelectedIndex,ElementName=listBox}"/>
</Grid>
XAML对应的后台代码:
public partial class MainWindow : Window
{
List<img> listImg = new List<img>();
public MainWindow()
{
listImg.Add(new img("Alpaca", @"Assets\img\Alpaca.jpg"));
listImg.Add(new img("Elephas", @"Assets\img\Elephas.jpg"));
listImg.Add(new img("Giraffa", @"Assets\img\Giraffa.jpg"));
listImg.Add(new img("LemurCatta", @"Assets\img\LemurCatta.jpg"));
listImg.Add(new img("panda", @"Assets\img\panda.jpg"));
listImg.Add(new img("pen", @"Assets\img\pen.jpg"));
InitializeComponent();
this.listBox.ItemsSource = listImg;
this.listBox.DisplayMemberPath = "Name";//Source="{Binding ElementName=listBox,Path=SelectItem.URL}"
//this.img.SetBinding(this.listBox.SelectedItem,new Binding()
//tb.Text = listBox.SelectedIndex.ToString();
}
} 展开
class img
{
public String Name { get; set; }
public String URL { get; set; }
public img(String name,String url) {
this.Name = name;
this.URL = url;
}
}
XAML代码:
<Grid>
<Button Content="Clear" ">
<ListBox Name="listBox" />
<Image x:Name="img" Source="Assets/img/Alpaca.jpg" />
<TextBlock x:Name="tb" HorizontalAlignment="Left" Margin="276,280,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding SelectedIndex,ElementName=listBox}"/>
</Grid>
XAML对应的后台代码:
public partial class MainWindow : Window
{
List<img> listImg = new List<img>();
public MainWindow()
{
listImg.Add(new img("Alpaca", @"Assets\img\Alpaca.jpg"));
listImg.Add(new img("Elephas", @"Assets\img\Elephas.jpg"));
listImg.Add(new img("Giraffa", @"Assets\img\Giraffa.jpg"));
listImg.Add(new img("LemurCatta", @"Assets\img\LemurCatta.jpg"));
listImg.Add(new img("panda", @"Assets\img\panda.jpg"));
listImg.Add(new img("pen", @"Assets\img\pen.jpg"));
InitializeComponent();
this.listBox.ItemsSource = listImg;
this.listBox.DisplayMemberPath = "Name";//Source="{Binding ElementName=listBox,Path=SelectItem.URL}"
//this.img.SetBinding(this.listBox.SelectedItem,new Binding()
//tb.Text = listBox.SelectedIndex.ToString();
}
} 展开
展开全部
对象的定义如下:
class img
{
public String Name { get; set; }
public String URL { get; set; }
public img(String name,String url) {
this.Name = name;
this.URL = url;
}
}
XAML代码:
<Grid>
<Button Content="Clear" ">
<ListBox Name="listBox" />
<Image x:Name="img" Source="Assets/img/Alpaca.jpg" />
<TextBlock x:Name="tb" HorizontalAlignment="Left" Margin="276,280,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding SelectedIndex,ElementName=listBox}"/>
</Grid>
XAML对应的后台代码:
public partial class MainWindow : Window
{
List<img> listImg = new List<img>();
public MainWindow()
{
listImg.Add(new img("Alpaca", @"Assets\img\Alpaca.jpg"));
listImg.Add(new img("Elephas", @"Assets\img\Elephas.jpg"));
listImg.Add(new img("Giraffa", @"Assets\img\Giraffa.jpg"));
listImg.Add(new img("LemurCatta", @"Assets\img\LemurCatta.jpg"));
listImg.Add(new img("panda", @"Assets\img\panda.jpg"));
listImg.Add(new img("pen", @"Assets\img\pen.jpg"));
InitializeComponent();
this.listBox.ItemsSource = listImg;
this.listBox.DisplayMemberPath = "Name";//Source="{Binding ElementName=listBox,Path=SelectItem.URL}"
//this.img.SetBinding(this.listBox.SelectedItem,new Binding()
//tb.Text = listBox.SelectedIndex.ToString();
}
}
class img
{
public String Name { get; set; }
public String URL { get; set; }
public img(String name,String url) {
this.Name = name;
this.URL = url;
}
}
XAML代码:
<Grid>
<Button Content="Clear" ">
<ListBox Name="listBox" />
<Image x:Name="img" Source="Assets/img/Alpaca.jpg" />
<TextBlock x:Name="tb" HorizontalAlignment="Left" Margin="276,280,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding SelectedIndex,ElementName=listBox}"/>
</Grid>
XAML对应的后台代码:
public partial class MainWindow : Window
{
List<img> listImg = new List<img>();
public MainWindow()
{
listImg.Add(new img("Alpaca", @"Assets\img\Alpaca.jpg"));
listImg.Add(new img("Elephas", @"Assets\img\Elephas.jpg"));
listImg.Add(new img("Giraffa", @"Assets\img\Giraffa.jpg"));
listImg.Add(new img("LemurCatta", @"Assets\img\LemurCatta.jpg"));
listImg.Add(new img("panda", @"Assets\img\panda.jpg"));
listImg.Add(new img("pen", @"Assets\img\pen.jpg"));
InitializeComponent();
this.listBox.ItemsSource = listImg;
this.listBox.DisplayMemberPath = "Name";//Source="{Binding ElementName=listBox,Path=SelectItem.URL}"
//this.img.SetBinding(this.listBox.SelectedItem,new Binding()
//tb.Text = listBox.SelectedIndex.ToString();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
意思listbox选同图片路径image控件预览
绑定要注意选值string类型图片sourceBitmapImage所要定义Converter转换
绑定要注意选值string类型图片sourceBitmapImage所要定义Converter转换
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询