如何在ComboBox中为一项触发这2个活动? [英] How to trigger these 2 activities for one item in ComboBox?

查看:74
本文介绍了如何在ComboBox中为一项触发这2个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定该怎么做.希望我能有所帮助.

我有一个组合框,上面有一些物品.单击某个项目时,我希望标签/文本框显示该项目的描述并播放视频.我已经使用VideoControl完成了视频部分.现在的问题是,如何为一项触发这两个活动?我应该在c#中使用if/else语句吗?

解决方案

由于我没有您的VideoControl,因此我使用MediaElement在下面创建示例.

注意:对于test.mpg,test2.mpg,test3.mpg,构建操作为无".并且复制到输出目录是如果更新则复制"

标记:
< Window x:Class ="TriggerTwoActivitiesForComboBoxItem.Window1"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation " ;
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml''
标题="Window1"高度="300".宽度="300". Background =``DarkGray''>
< Grid>
< Grid.RowDefinitions<
="自动></RowDefinition/>
</Grid.RowDefinitions>
< ComboBox Name =" combobox" ItemsSource ="{Binding}" DisplayMemberPath =描述". SelectionChanged ="combobox_SelectionChanged"/>
< TextBlock Grid.Row ="1" Text ="{Binding ElementName = combobox,Path = Text}"/< Border Grid.Row ="2" BorderBrush ="DarkGray" BorderThickness ="1". CornerRadius ="2">
< MediaElement x:Name =视频" LoadedBehavior ="Manual" Stretch =``填充''</MediaElement>
</Border>
</Grid>
</Window>

代码:
使用System;
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;

命名空间TriggerTwoActivitiesForComboBoxItem
{
////< summary>
////Window1.xaml的交互逻辑
///</summary>
public局部类Window1:Window
{
public Window1()
初始化{InitializeComponent();
DataContext = new Items();
>

私有void combobox_SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
项item =(Item)e.AddedItems [0]; UriKind.Relative);
video.Position = TimeSpan.Zero;
}
}

public class Items:列表< Item>
{{br/> public public()
Add(new Item(){描述=第一个视频",VideoSource = " test.mpg"});
Add(new Item(){Description ="second video",VideoSource ="test2.mpg"});
Add(newItem(){说明=第三视频",VideoSource ="test3.mpg"});
}
}
公共类Item
{
公用字符串描述{set ;得到; }
公用字符串VideoSource {设置;得到; }
}
}


I m not sure how to do this. Hope I'll get some help.

I have a ComboBox with some items on it. When an item is clicked, I want a label/textbox to display a description abt the item and play a video as well. I have done the video part using a VideoControl. Now the problem is, how do I trigger these 2 activities for one item? And should I use a if/else statement in c#?

解决方案

I use MediaElement to create example below since I don't have your VideoControl. 

Note: for test.mpg, test2.mpg, test3.mpg, Build Action is "None" and Copy to Output Directory is "Copy if newer"

Markup:
<Window x:Class="TriggerTwoActivitiesForComboBoxItem.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Background="DarkGray">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ComboBox Name="combobox" ItemsSource="{Binding}" DisplayMemberPath="Description" SelectionChanged="combobox_SelectionChanged"/>
        <TextBlock Grid.Row="1" Text="{Binding ElementName=combobox, Path=Text}"/>
        <Border Grid.Row="2" BorderBrush="DarkGray" BorderThickness="1" CornerRadius="2">
            <MediaElement x:Name="video" LoadedBehavior="Manual" Stretch="Fill"></MediaElement>
        </Border>
    </Grid>
</Window>

Code:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace TriggerTwoActivitiesForComboBoxItem
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = new Items();
        }

        private void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Item item = (Item)e.AddedItems[0];
            video.Source = new Uri(item.VideoSource, UriKind.Relative);
            video.Position = TimeSpan.Zero;
            video.Play();
        }
    }

    public class Items : List<Item>
    {
        public Items()
        {
            Add(new Item() { Description = "first video", VideoSource = "test.mpg" });
            Add(new Item() { Description = "second video", VideoSource = "test2.mpg" });
            Add(new Item() { Description = "third video", VideoSource = "test3.mpg" });
        }
    }
    public class Item
    {
        public string Description { set; get; }
        public string VideoSource { set; get; }
    }
}


这篇关于如何在ComboBox中为一项触发这2个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆