WPF传递包括object在内的几个参数

时间:2018-01-05 01:07:00

标签: c# wpf data-binding parameters

我有简单的按钮,可以像编辑元素一样执行特定操作。我想传递一些参数,包括来自datagrid的特定对象SelectedMatch和来自callendar的SelectedDate。现在我有这样的事情:

<DatePicker x:Name="dpEditDateMatch" SelectedDateFormat="Long" SelectedDate="{Binding MyDateTimeProperty2, Mode=TwoWay}" ></DatePicker>
                    <Button Content="Edit match" Command="{Binding EditMatchCommand}">
                        <Button.CommandParameter>
                            <MultiBinding Converter="{StaticResource Converter}">
                                <Binding >??</Binding>
                                <Binding Path="SelectedDate" ElementName="dpEditDateMatch"/>
                            </MultiBinding>
                        </Button.CommandParameter>
                    </Button>
                    <ScrollViewer>
                        <DataGrid IsReadOnly="True" ItemsSource="{Binding match}" SelectedItem="{Binding SelectedMatch}"/>
                    </ScrollViewer>

在.cs文件中,我有像这样定义的SelectedMatch:

object _SelectedMatch;
    public object SelectedMatch
    {
        get
        {
            return _SelectedMatch;
        }
        set
        {
            if (_SelectedMatch != value)
            {
                _SelectedMatch = value;
                RaisePropertyChanged("SelectedMatch");
            }
        }
    }

我该怎么做?我的意思是,从xaml处理这个。

2 个答案:

答案 0 :(得分:0)

我刚遇到你昨天说的问题。

我现在解决了。

这是XAML:

<Page.Resources>
            <local:MultiIcommandParameterConverter x:Key="MultiIcommandParameterConverter"></local:MultiIcommandParameterConverter>
    </Page.Resources>
 <ItemsControl>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel></WrapPanel>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Button Command="{Binding ButtonClick}">
                                        <Button.CommandParameter>
                                            <MultiBinding Converter="{StaticResource MultiIcommandParameterConverter}">
                                                <Binding Path="DetailIndex"/>
                                                <Binding Path="DetailContent"/>
                                            </MultiBinding>
                                        </Button.CommandParameter>                                       
                                    </Button>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>

<
这里是INotifyPropertyChanged背后的代码:

 public class DetailButtonClass: INotifyPropertyChanged
            {
                public event PropertyChangedEventHandler PropertyChanged;
                private void NotifyPropertyChange(string propertyName)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    }
                }           
                string _DetailContent;
                public string DetailContent
                {
                    get
                    {
                        return _DetailContent;
                    }
                    set
                    {
                        _DetailContent = value;                    
                        NotifyPropertyChange("DetailContent");
                    }
                }
                string _DetailIndex;
                public string DetailIndex
                {
                    get
                    {
                        return _DetailIndex;
                    }
                    set
                    {
                        _DetailIndex = value;
                        NotifyPropertyChange("DetailIndex");
                    }
                }           
                CommandClass _ButtonClick;
                public override CommandClass ButtonClick
                {
                    get
                    {
                        return _ButtonClick;
                    }
                    set
                    {
                        _ButtonClick = value;
                        NotifyPropertyChange("ButtonClick");
                    }
                }
 public class CommandClass : ICommand
        {            
            public event EventHandler CanExecuteChanged;

            public bool CanExecute(object parameter)
            {
                return true;
            }
            public delegate void DelegateClickVoid(string index,string content);
            public DelegateClickVoid ClickVoid = null;            
            public void Execute(object parameter)
            {
                string[] parameterArray = parameter.ToString().Split(",".ToArray());
                ClickVoid?.Invoke(parameterArray[0], parameterArray[1]);
            }

        }
            }

<
这里有关于IMultiValueConverter的代码,最重要的是:

public class MultiIcommandParameterConverter : IMultiValueConverter
{        
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values[0] + "," + values[1];
    }

    object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}



完全地,使用&#39;&#39将所有参数转换为一个参数;拆分它。点击按钮时,将参数拆分为&#39;,&#39;参数。

答案 1 :(得分:0)

您已经拥有const initialState = { isAuthenticated: false} const authReducer = (state = initialState, action) => { switch(action.type){ case ('USER_AUTHENTICATED'): { return Object.assign({}, state, { isAuthenticated: true, userPermissions: action.userInfo.userPermissions, instanceType: action.userInfo.instanceType } ) } case ('INVALID_CREDENTIALS'): { return Object.assign({}, state, { isAuthenticated:false } ) } case ('LOG_OUT'): { return initialState } default: return state } } const rootReducer = combineReducers({ authReducer, routerReducer }) export default rootReducer 中的数据,即ViewModel,该数据适用于您的日期,MyDateTimeProperty2代表该数据。在你的MultiBinding中你应该传递这个:

SelectedMatch

因为Button已经与<Button Content="Edit match" Command="{Binding EditMatchCommand}"> <Button.CommandParameter> <MultiBinding Converter="{StaticResource Converter}"> <Binding Path="MyDateTimeProperty2" /> <Binding Path="SelectedMatch" /> </MultiBinding> </Button.CommandParameter> </Button> DataContext具有相同的DatePicker,您甚至不需要DataGrid上的x:Name="dpEditDateMatch"来获取数据。