wpf ObjectDataProvider 方法参数绑定到组合框选定的值

发布于 2024-10-24 02:18:49 字数 5090 浏览 7 评论 0原文

我正在处理 WPF 绑定中的问题。 我正在创建一个用户控件,它显示一个数据网格,并按 2 个可能的值进行过滤。 第一个值由文本框设置,第二个值由组合框设置。 我正在使用 ObjectDataProvider 来映射具有 2 个参数的方法,并且文本框和组合框应设置这 2 个参数。 这是代码。

<UserControl x:Class="VisualHorse.Corse"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:VisualHorse"
         xmlns:system="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="530" Loaded="UserControl_Loaded">
<UserControl.Resources>
    <ObjectDataProvider x:Key="HorseDataProvider"
             ObjectType="{x:Type local:HorseDataProvider}"
             MethodName="GetCorse" >
        <ObjectDataProvider.MethodParameters>
            <x:Static Member="system:String.Empty" />
            <x:Static Member="system:String.Empty" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</UserControl.Resources>
<StackPanel >
    <Grid  Name="GRIDFilter" Height="50">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Label Content="Data corsa" HorizontalAlignment="Center" Name="label1" VerticalAlignment="Center" />
        <Label Content="Località" Grid.Column="1" HorizontalAlignment="Center" Name="label2" VerticalAlignment="Center" />
        <Label Content="N° Corsa" Grid.Column="2" HorizontalAlignment="Center" Name="label3" VerticalAlignment="Center" />
        <Button Content="Filtra" Grid.Column="3" Grid.RowSpan="2" Width="50" Height="30" />
        <DatePicker Grid.Row="1"  HorizontalAlignment="Center" Name="DPDataCorsa" VerticalAlignment="Center" Width="115" />
        <ComboBox Grid.Column="1" Grid.Row="1"  HorizontalAlignment="Stretch" Name="CBlocalita" VerticalAlignment="Center" Margin="5,0" >
            <ComboBox.SelectedValue>
                <Binding Source="{StaticResource HorseDataProvider}"
                 Path="MethodParameters[0]"
                 BindsDirectlyToSource="true"/>
            </ComboBox.SelectedValue>
        </ComboBox>
        <TextBox Name="TBNumCorsa" Grid.Column="2" Grid.Row="1" Margin="5,0" >
            <Binding Source="{StaticResource HorseDataProvider}"
                 Path="MethodParameters[1]"
                 BindsDirectlyToSource="true"
                 UpdateSourceTrigger="PropertyChanged" />
        </TextBox>
    </Grid>

    <DataGrid Name="DGCorse" ItemsSource="{Binding Source={StaticResource HorseDataProvider}}" AutoGenerateColumns="False" Margin="0,10,0,0" CanUserResizeRows="False" SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Localita.descrizione}" Header="Località" />
            <DataGridTextColumn Binding="{Binding Path=data, StringFormat=\{0:d\}}" Header="Data Corsa" />
            <DataGridTextColumn Binding="{Binding Path=numero}" Header="N° Corsa" />
            <DataGridTextColumn Binding="{Binding Path=TipoCorsa.descrizione}" Header="Tipo corsa" />
            <DataGridTextColumn Binding="{Binding Path=TipoTerreno.descrizione}" Header="Terreno" />
            <DataGridTextColumn Binding="{Binding Path=TipoFantino.descrizione}" Header="Tipo fantino" />
            <DataGridTextColumn Binding="{Binding Path=premio, StringFormat=\{0:c\}}" Header="Premio" />
            <DataGridTextColumn Binding="{Binding Path=distacchi}" Header="Distacchi" />
            <DataGridTextColumn Binding="{Binding Path=distanza}" Header="Distanza" />
            <DataGridTextColumn Binding="{Binding Path=TipoPista.descrizione}" Header="Pista" />
            <DataGridTextColumn Binding="{Binding Path=Eta.descrizione}" Header="Età" />
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

有什么问题吗? 如果我只绑定文本框属性,一切都会正常工作,但尝试将 Combobox.SeletedValue 属性绑定到第一个方法参数时会抛出异常(由 wpf 引擎默默处理):

System.Windows.Data 错误:35: ObjectDataProvider:尝试失败 调用类型上的方法; 方法='GetCorse'; 类型='HorseDataProvider';错误='否 找到匹配的方法 参数签名。' MissingMethodException:'System.MissingMethodException: 方法 'VisualHorse.HorseDataProvider.GetCorse' 未找到。在 System.RuntimeType.InvokeMember(字符串 名称,BindingFlags 绑定标志, Binder活页夹,对象目标,对象[] 提供的参数,参数修改器[] 修饰符、CultureInfo 文化、 String[] 命名参数)位于 System.Windows.Data.ObjectDataProvider.InvokeMethodOnInstance(异常& e)'

任何帮助将不胜感激

I'm dealing with a problem in WPF binding.
I'm creating a user control which present a datagrid, fiiltered by 2 possible values.
The first value is set by a textbox, the second one by a combo box.
I'm using an ObjectDataProvider to map a methos with 2 parameters, and the textbox and the combobox should set these 2 parameters.
Here's the code.

<UserControl x:Class="VisualHorse.Corse"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:VisualHorse"
         xmlns:system="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="530" Loaded="UserControl_Loaded">
<UserControl.Resources>
    <ObjectDataProvider x:Key="HorseDataProvider"
             ObjectType="{x:Type local:HorseDataProvider}"
             MethodName="GetCorse" >
        <ObjectDataProvider.MethodParameters>
            <x:Static Member="system:String.Empty" />
            <x:Static Member="system:String.Empty" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</UserControl.Resources>
<StackPanel >
    <Grid  Name="GRIDFilter" Height="50">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Label Content="Data corsa" HorizontalAlignment="Center" Name="label1" VerticalAlignment="Center" />
        <Label Content="Località" Grid.Column="1" HorizontalAlignment="Center" Name="label2" VerticalAlignment="Center" />
        <Label Content="N° Corsa" Grid.Column="2" HorizontalAlignment="Center" Name="label3" VerticalAlignment="Center" />
        <Button Content="Filtra" Grid.Column="3" Grid.RowSpan="2" Width="50" Height="30" />
        <DatePicker Grid.Row="1"  HorizontalAlignment="Center" Name="DPDataCorsa" VerticalAlignment="Center" Width="115" />
        <ComboBox Grid.Column="1" Grid.Row="1"  HorizontalAlignment="Stretch" Name="CBlocalita" VerticalAlignment="Center" Margin="5,0" >
            <ComboBox.SelectedValue>
                <Binding Source="{StaticResource HorseDataProvider}"
                 Path="MethodParameters[0]"
                 BindsDirectlyToSource="true"/>
            </ComboBox.SelectedValue>
        </ComboBox>
        <TextBox Name="TBNumCorsa" Grid.Column="2" Grid.Row="1" Margin="5,0" >
            <Binding Source="{StaticResource HorseDataProvider}"
                 Path="MethodParameters[1]"
                 BindsDirectlyToSource="true"
                 UpdateSourceTrigger="PropertyChanged" />
        </TextBox>
    </Grid>

    <DataGrid Name="DGCorse" ItemsSource="{Binding Source={StaticResource HorseDataProvider}}" AutoGenerateColumns="False" Margin="0,10,0,0" CanUserResizeRows="False" SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Localita.descrizione}" Header="Località" />
            <DataGridTextColumn Binding="{Binding Path=data, StringFormat=\{0:d\}}" Header="Data Corsa" />
            <DataGridTextColumn Binding="{Binding Path=numero}" Header="N° Corsa" />
            <DataGridTextColumn Binding="{Binding Path=TipoCorsa.descrizione}" Header="Tipo corsa" />
            <DataGridTextColumn Binding="{Binding Path=TipoTerreno.descrizione}" Header="Terreno" />
            <DataGridTextColumn Binding="{Binding Path=TipoFantino.descrizione}" Header="Tipo fantino" />
            <DataGridTextColumn Binding="{Binding Path=premio, StringFormat=\{0:c\}}" Header="Premio" />
            <DataGridTextColumn Binding="{Binding Path=distacchi}" Header="Distacchi" />
            <DataGridTextColumn Binding="{Binding Path=distanza}" Header="Distanza" />
            <DataGridTextColumn Binding="{Binding Path=TipoPista.descrizione}" Header="Pista" />
            <DataGridTextColumn Binding="{Binding Path=Eta.descrizione}" Header="Età" />
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

What's wrong with it?
Everything works fine if I just bind the textbox property, but trying to bind the Combobox.SeletedValue property to the first method parameter it throws an exception (silently handled by the wpf engine):

System.Windows.Data Error: 35 :
ObjectDataProvider: Failure trying to
invoke method on type;
Method='GetCorse';
Type='HorseDataProvider'; Error='No
method was found with matching
parameter signature.'
MissingMethodException:'System.MissingMethodException:
Method
'VisualHorse.HorseDataProvider.GetCorse'
not found. at
System.RuntimeType.InvokeMember(String
name, BindingFlags bindingFlags,
Binder binder, Object target, Object[]
providedArgs, ParameterModifier[]
modifiers, CultureInfo culture,
String[] namedParams) at
System.Windows.Data.ObjectDataProvider.InvokeMethodOnInstance(Exception&
e)'

Any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-10-31 02:18:49

好的,我在我的方法中发现了问题。
简而言之,我没有意识到 ComboBox.SelectedValue 类型取决于我如何填充组合框(我在后面的代码中所做的)。
只需按以下方式修改 ObjectDataProvider 定义即可解决问题:

<ObjectDataProvider x:Key="HorseDataProvider"
             ObjectType="{x:Type local:HorseDataProvider}"
             MethodName="GetCorse" >
        <ObjectDataProvider.MethodParameters>                
            <system:Int32>0</system:Int32>
            <x:Static Member="system:String.Empty" />                 
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

这样,ComboBox.SelectedItem 就会绑定到 Int32 方法参数。

Ok, I found the issue on my approach.
Simply, I didn't get that the ComboBox.SelectedValue type depends on how I populate the combobox (wich I did in code behind).
Simply modifying the ObjectDataProvider definition in the following way, resolved the problem:

<ObjectDataProvider x:Key="HorseDataProvider"
             ObjectType="{x:Type local:HorseDataProvider}"
             MethodName="GetCorse" >
        <ObjectDataProvider.MethodParameters>                
            <system:Int32>0</system:Int32>
            <x:Static Member="system:String.Empty" />                 
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

This way the ComboBox.SelectedItem is bound to an Int32 method parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文