简体   繁体   English

C# WPF 数据绑定

[英]C# WPF DataBinding

Im new in wpf programming and i have an application with MVVM.我是 wpf 编程的新手,我有一个带有 MVVM 的应用程序。 I have a StackPanel and inside the StackPanel there is a ItemsControl named ListView and the Itemsource is my ObserveCollection NewProducts which conatins ProductID and Name.我有一个 StackPanel,在 StackPanel 里面有一个名为 ListView 的 ItemsControl,Itemsource 是我的 ObserveCollection NewProducts,它包含 ProductID 和 Name。

So what is my code does, well it generates buttons with Names from my Collection NewProducts and i wanna give this buttons Command that triggers when i click on them.那么我的代码是做什么的,它从我的 Collection NewProducts 中生成带有名称的按钮,我想给这个按钮命令,当我点击它们时触发它们。

I try this:我试试这个:

<StackPanel Grid.Row="1">
    <ItemsControl x:Name="ListViewProducts" ItemsSource="{Binding NewProducts}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border Margin="10" Width="auto" Height="auto">
                    <StackPanel>
                        <Border Width="100" Height="40" CornerRadius="5">
                            
                            <Button Content="{Binding Name}" Tag="{Binding ProductId}" FontWeight="Bold" Command="{BindingSaleViewModel.SendCommand}"/>
                            
                        </Border>
                       
                    </StackPanel>
                </Border>
            </DataTemplate>
         
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</StackPanel>

But when i run my program and when i click on my buttons nothing happens.但是当我运行我的程序并且当我点击我的按钮时没有任何反应。 Its because the Command that i have in my SaleViewModel was not found in Product which is my Model class.这是因为我在 SaleViewModel 中的命令在我的 Model class 的产品中找不到。 And i wanna know if i can somehow get the path to my Command in my ViewModel.而且我想知道我是否可以在我的 ViewModel 中以某种方式获得我的命令的路径。

Thanks, and please forgive me mine English, I know its bad.谢谢,请原谅我的英语,我知道它不好。

Binding methods in WPF is unfortunately not that simple.不幸的是,WPF 中的绑定方法并不是那么简单。 The command is actually a class, that implements the ICommand interface.该命令实际上是一个 class,它实现了ICommand接口。

You can find an example here: How to Bind a Command in WPF .您可以在此处找到示例: 如何在 WPF 中绑定命令

Otherwise you can bind the click event to a method in code-behind <Button Click="Button_Click"/> In code-behind:否则,您可以将单击事件绑定到代码隐藏中的方法<Button Click="Button_Click"/>在代码隐藏中:

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Add your code here...
}

Give your root element a name.给你的根元素一个名字。 eg:例如:

<UserControl x:Class="blah" 
             other stuff
             x:Name="root">

Then you can reference the root element in your binding, who's datacontext will (presumably?) be your SaleViewModel:然后您可以在绑定中引用根元素,谁的 datacontext 将(大概?)成为您的 SaleViewModel:

<Button Content="{Binding Name}" Tag="{Binding ProductId}" FontWeight="Bold" 
        Command="{Binding DataContext.SendCommand, ElementName=root}"
        CommandParameter="{Binding}"/>

Also note the CommandParameter binding which will pass the button's data context (Product) through to your command, otherwise you won't know which product the command relates to.另请注意CommandParameter绑定,它将按钮的数据上下文(产品)传递给您的命令,否则您将不知道该命令与哪个产品相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM