小编Mar*_*tin的帖子

将函数绑定到xaml中的MouseDown事件?

我正在努力学习WPF和C#编程,但我正在努力理解绑定等.

我被困在"绑定"函数或命令到我的网格中的XAML对象.

<Image x:Name="BlkRook1" Source="../Data/BlkRook.png" Grid.Row="{Binding Path=ChessPieces[0].Row}" Grid.Column="{Binding Path=ChessPieces[0].Column}" MouseDown="{Binding Path=ChessPieces[0].Move()}"/>
Run Code Online (Sandbox Code Playgroud)

代码MouseDown="{Binding Path=ChessPieces[0].Move()}"不起作用,但显示我试图实现的内容

在我的viewmodel中,我已经定义了一个列表,我已经填充了我不同的棋子的实例,然后我计划将每个图像绑定到该列表中的实例.

我试图做的是绑定MouseDown函数此刻,似乎如果我将该函数放在Mainwindow.xaml.cs文件中,它可以访问它.但我希望它能够从模型中访问它

Mainwindow.xaml.cs

namespace TheGame.Views
{
    public partial class MainWindow : Window   
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ChessBoardViewModel();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

BlkRook对象定义了行,列,名称和函数移动.

错误信息:

"'TheGame.Views.MainWindow'不包含'Move'的定义,也没有扩展方法'Move'接受类型为'TheGame.Views.MainWindow'的第一个参数'(你是否缺少using指令或程序集)参考?)"

那么..如何将Model-object中定义的函数绑定到XAML对象?

谢谢

/马丁

c# wpf xaml mvvm

5
推荐指数
1
解决办法
1万
查看次数

在C#View Model中访问XAML对象

我在我的View xaml代码中设置了一个Grid.它由一个填充64个矩形和32个图像的网格组成.我有一个模型类,我想将一些属性绑定到XMAL-opjects属性的图像.ChessBoardViewModel.cs中的代码:

foreach (ChessPiece Piece in ChessPieces)
{
    Image Img = ChessBoard.FindName(Piece.Name) as Image;

    Binding RowBinding = new Binding("Row");
    RowBinding.Source = Piece;
    Img.SetBinding(Grid.RowProperty, RowBinding);

    Binding ColumnBinding = new Binding("Column");
    ColumnBinding.Source = Piece;
    Img.SetBinding(Grid.ColumnProperty, ColumnBinding);
}
Run Code Online (Sandbox Code Playgroud)

Chessboard是包含所有图像的Grid的名称.

我的计划是应该将图像绑定到每个实例行和列值,以确定它们在游戏中的位置.

问题是,即使我已经包含了Views命名空间,我也无法出于某种原因访问XAML对象.

编辑:

好的,这是我的代码,以及我如何计划现在这样做:

<Grid x:Name="ChessBoard">
  <Rectangle x:Name="A1" Fill="Black" Grid.Column="1" />
  ...
  ...
  <Image x:Name="BlkRook1" Source="../Data/BlkRook.png" Grid.Row="{Binding ChessBoardViewModel.ChessPieces[0].Row}" Grid.Column="{Binding ChessPieces[0].Column}"/>
 ...   
 </Grid>
Run Code Online (Sandbox Code Playgroud)

和Viewmodel包含我的ChessPieces列表

ChessPieces.Add(new BlkRook() { Name = "BlkRook1", Row = 0, Column = 0 });
Run Code Online (Sandbox Code Playgroud)

我想将XAML图像Grid.Row和Grid.Column属性绑定到我的对象的正确实例.但似乎无法找到实现它的方法.

c# wpf xaml

3
推荐指数
1
解决办法
1644
查看次数

单击填充了网格的Uniformgrid,获取网格的名称

我是WPF的新手,我正在使用这段代码用网格填充uniformgrid,

 public MainWindow()  
                {  
                    InitializeComponent();  
                    SolidColorBrush defaultBrush = new SolidColorBrush(Colors.Wheat);  
                    SolidColorBrush alternateBrush = new SolidColorBrush(Colors.Black);
                    Char L = 'A';
                    int N = 1;  
                    for (int i = 0; i < 64; i++)  
                    {  
                        Grid cell = new Grid();
                        if(N==9)
                         {   
                            N=1;
                            L++;
                         }

                        if ((i + i / 8) % 2 == 0)  
                        {  
                            cell.Name = L + N.ToString();
                            cell.Background = defaultBrush;  
                            ChessBoard.Children.Add(cell);  
                        }  
                        else  
                        {  
                            cell.Name = L + N.ToString();
                            cell.Background = alternateBrush;   
                            ChessBoard.Children.Add(cell);  
                        } 
                        N++
                    } 
Run Code Online (Sandbox Code Playgroud)

然后,当我点击名为ChessBoard的uniformedgrid时,我试图找出某个网格的名称.

       private …
Run Code Online (Sandbox Code Playgroud)

c# wpf uniformgrid

2
推荐指数
1
解决办法
1953
查看次数

标签 统计

c# ×3

wpf ×3

xaml ×2

mvvm ×1

uniformgrid ×1