none
WPF ComboBox Command Binding 根据选择ComboBox不同ITEM控制TextBox显示不同的值 最好DEMO THANKS RRS feed

  • 问题

  • WPF ComboBox Command Binding 根据选择ComboBox不同ITEM控制TextBox显示不同的值 最好DEMO THANKS

    选择ComboBox时候 无需绑定数据源 选择之后刷新 TextBox 即可 需要命令实现 绑定TextBox

    2018年10月30日 11:09

答案

  • Hi   NFT_X,

    >>WPF ComboBox Command Binding 根据选择ComboBox不同ITEM控制TextBox显示不同的值 最好DEMO THANKS

    选择ComboBox时候 无需绑定数据源 选择之后刷新 TextBox 即可 需要命令实现 绑定TextBox

    请参考下面的例子。

    前提引用:Microsoft.Expression.Interactivity.DLL,System.Windows.Interactivity;
    前台添加: xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

    XAML:

    <Window x:Class="Wpf_Example.Comboxwithcomand"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Wpf_Example"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            mc:Ignorable="d"
            Title="Comboxwithcomand" Height="450" Width="800">
        <Grid>
            <ComboBox x:Name="comboBox" Grid.Column="1" Height="30" Width="250" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <local:ExecuteCommandAction Command="{Binding Clicked}" CommandParameter="{Binding ElementName=comboBox}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ComboBoxItem Name="Item1">Item1</ComboBoxItem>
                <ComboBoxItem Name="Item2">Item2</ComboBoxItem>
                <ComboBoxItem Name="Item3">Item3</ComboBoxItem>
                <ComboBoxItem Name="Item4">Item4</ComboBoxItem>
                <ComboBoxItem Name="Item5">Item5</ComboBoxItem>
                <ComboBoxItem Name="Item6">Item6</ComboBoxItem>
            </ComboBox>
            <TextBox x:Name="showtxt" HorizontalAlignment="Left" Height="23" Margin="271,117,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250"/>
        </Grid>
    </Window>

    XAML.CS:

    /// <summary>
        /// Interaction logic for Comboxwithcomand.xaml
        /// </summary>
        public partial class Comboxwithcomand : Window
        {
            public Comboxwithcomand()
            {
                InitializeComponent();
                Clicked = new ActionCommand(Click);
                this.DataContext = this;
            }
    
            public ICommand Clicked { get; private set; }
    
            public void Click(object arg)
            {
                var SS = arg as ComboBox;
                if (SS == null ) return;
                ComboBoxItem sitem = (ComboBoxItem)SS.SelectedItem;
                showtxt.Text= sitem.Content.ToString();
                
            }
        }
    
        public class ExecuteCommandAction : TargetedTriggerAction<UIElement>
        {
            public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));
            /// <summary> 命令
            /// </summary>
            public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
    
    
            public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));
            /// <summary> 
            /// </summary>
            public object CommandParameter
            {
                get { return GetValue(CommandParameterProperty); }
                set { SetValue(CommandParameterProperty, value); }
            }
    
            /// <summary>
            /// </summary>
            protected override void Invoke(object parameter)
            {
                if (Command == null) return;
                if (Command.CanExecute(CommandParameter))
                {
                    Command.Execute(CommandParameter);
                }
            }
        }
    
    


    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年10月31日 4:12
    版主

全部回复

  • Hi   NFT_X,

    >>WPF ComboBox Command Binding 根据选择ComboBox不同ITEM控制TextBox显示不同的值 最好DEMO THANKS

    选择ComboBox时候 无需绑定数据源 选择之后刷新 TextBox 即可 需要命令实现 绑定TextBox

    请参考下面的例子。

    前提引用:Microsoft.Expression.Interactivity.DLL,System.Windows.Interactivity;
    前台添加: xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

    XAML:

    <Window x:Class="Wpf_Example.Comboxwithcomand"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Wpf_Example"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            mc:Ignorable="d"
            Title="Comboxwithcomand" Height="450" Width="800">
        <Grid>
            <ComboBox x:Name="comboBox" Grid.Column="1" Height="30" Width="250" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <local:ExecuteCommandAction Command="{Binding Clicked}" CommandParameter="{Binding ElementName=comboBox}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ComboBoxItem Name="Item1">Item1</ComboBoxItem>
                <ComboBoxItem Name="Item2">Item2</ComboBoxItem>
                <ComboBoxItem Name="Item3">Item3</ComboBoxItem>
                <ComboBoxItem Name="Item4">Item4</ComboBoxItem>
                <ComboBoxItem Name="Item5">Item5</ComboBoxItem>
                <ComboBoxItem Name="Item6">Item6</ComboBoxItem>
            </ComboBox>
            <TextBox x:Name="showtxt" HorizontalAlignment="Left" Height="23" Margin="271,117,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250"/>
        </Grid>
    </Window>

    XAML.CS:

    /// <summary>
        /// Interaction logic for Comboxwithcomand.xaml
        /// </summary>
        public partial class Comboxwithcomand : Window
        {
            public Comboxwithcomand()
            {
                InitializeComponent();
                Clicked = new ActionCommand(Click);
                this.DataContext = this;
            }
    
            public ICommand Clicked { get; private set; }
    
            public void Click(object arg)
            {
                var SS = arg as ComboBox;
                if (SS == null ) return;
                ComboBoxItem sitem = (ComboBoxItem)SS.SelectedItem;
                showtxt.Text= sitem.Content.ToString();
                
            }
        }
    
        public class ExecuteCommandAction : TargetedTriggerAction<UIElement>
        {
            public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));
            /// <summary> 命令
            /// </summary>
            public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
    
    
            public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));
            /// <summary> 
            /// </summary>
            public object CommandParameter
            {
                get { return GetValue(CommandParameterProperty); }
                set { SetValue(CommandParameterProperty, value); }
            }
    
            /// <summary>
            /// </summary>
            protected override void Invoke(object parameter)
            {
                if (Command == null) return;
                if (Command.CanExecute(CommandParameter))
                {
                    Command.Execute(CommandParameter);
                }
            }
        }
    
    


    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年10月31日 4:12
    版主
  • 谢谢您的回答~
    2018年12月7日 1:59