Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在以下代码中正确使用MVVM/绑定,并且Icommand属性存在问题_C#_Wpf_Mvvm_Icommand - Fatal编程技术网

C# 无法在以下代码中正确使用MVVM/绑定,并且Icommand属性存在问题

C# 无法在以下代码中正确使用MVVM/绑定,并且Icommand属性存在问题,c#,wpf,mvvm,icommand,C#,Wpf,Mvvm,Icommand,对于使用MVVM的WPF编码来说是非常新的。尝试使用MVVM在WPF中制作一个简单的计算器。但无法在下面的代码中触发Icommand。如果可能,请帮助我。如果有人能帮我,我将不胜感激。 查看代码: <Window x:Class="MVVMCalculator.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schema

对于使用MVVM的WPF编码来说是非常新的。尝试使用MVVM在WPF中制作一个简单的计算器。但无法在下面的代码中触发Icommand。如果可能,请帮助我。如果有人能帮我,我将不胜感激。
查看代码:

<Window x:Class="MVVMCalculator.MainWindow"
        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:MVVMCalculator"
        mc:Ignorable="d"
        Title="Calculator" Height="350" Width="300">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="85"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBox Text="{Binding Display, Mode=OneWay}" IsReadOnly="True" TextWrapping="Wrap" 
                 Grid.Row="0" Background="#E2E2E2" Margin="0,10,0,0" VerticalAlignment="Top" 
                 Height="75" Width="250" HorizontalAlignment="Center" FontSize="22" FontWeight="Bold" 
                 TextAlignment="Right">
            <TextBox.Effect>
                <DropShadowEffect/>
            </TextBox.Effect>
        </TextBox>
        <ItemsControl Grid.Row="1" ItemsSource="{Binding Buttns}" Margin="15,15,15,10">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="5" Rows="4" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Txt, Mode=TwoWay}" Command="{Binding Enter_number}"

                            FontSize="18" FontWeight="Bold" Height="50" Width="50" Background="#eef2f3" 
                            BorderBrush="Black" BorderThickness="1.0" Name="number">
                        <Button.Effect>
                            <DropShadowEffect/>
                        </Button.Effect>
                    </Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>
Xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MVVMCalculator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间MVVMCalculator
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=newviewmodel();
}
}
}

由于在
ViewModel
类中定义了
Enter\u number
属性,因此需要使用
{RelativeSource}
才能绑定到该属性:

    <Button Content="{Binding Txt, Mode=TwoWay}"
            Command="{Binding DataContext.Enter_number, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
            FontSize="18" FontWeight="Bold" Height="50" Width="50" Background="#eef2f3" 
            BorderBrush="Black" BorderThickness="1.0" Name="number">
        <Button.Effect>
            <DropShadowEffect/>
        </Button.Effect>
    </Button>


按钮的默认
DataContext
ItemsControl
ItemsSource
集合中的当前
Buttons
对象,这就是绑定失败的原因。

因为
Enter\u number
属性是在
ViewModel
类中定义的,您需要使用
{RelativeSource}
要能够绑定到它:

    <Button Content="{Binding Txt, Mode=TwoWay}"
            Command="{Binding DataContext.Enter_number, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
            FontSize="18" FontWeight="Bold" Height="50" Width="50" Background="#eef2f3" 
            BorderBrush="Black" BorderThickness="1.0" Name="number">
        <Button.Effect>
            <DropShadowEffect/>
        </Button.Effect>
    </Button>


按钮的默认
DataContext
ItemsSource
集合
ItemsControl
中当前的
Buttons
对象,这就是绑定失败的原因。

请更详细地描述您的问题。运行时,我可以看到UI上有按钮,我可以调试程序直到是的,但当我单击任何按钮时,我无法调试程序,也看不到任何更改。可能是Icommand有问题!您正在设置private
display
,当您应该设置public
display
,这将触发UI更改。但这仅适用于字符串为
C
时。请在mor中描述您的问题e详细信息。当我运行时,我可以看到UI上有按钮,我可以调试程序,直到出现为止,但当我单击任何按钮时,我无法调试程序,并且看不到任何更改。可能是Icommand有问题!您正在设置private
display
何时应该设置public
display
,这将触发UI更改e、 但这仅适用于字符串为
C
的情况。非常感谢您的时间和建议。我已将其标记为答案。请将我发送到可以学习从UI到源数据绑定的链接。请参阅以下文章:非常感谢您的时间和建议。我已将其标记为答案。您能请将我转到我可以学习从UI到源数据绑定的链接。请参阅以下文章:
    <Button Content="{Binding Txt, Mode=TwoWay}"
            Command="{Binding DataContext.Enter_number, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
            FontSize="18" FontWeight="Bold" Height="50" Width="50" Background="#eef2f3" 
            BorderBrush="Black" BorderThickness="1.0" Name="number">
        <Button.Effect>
            <DropShadowEffect/>
        </Button.Effect>
    </Button>