简体   繁体   English

在 C# 视图模型中访问 XAML 对象

[英]Accessing XAML objects in C# View Model

i have set up a Grid in my View xaml code.我在我的 View xaml 代码中设置了一个网格。 it consists of a grid filled with 64 rectangles and 32 images.它由一个填充了 64 个矩形和 32 个图像的网格组成。 I have a model-class for the images i would like to bind some properties from to the XMAL-opjects properties.我有一个用于图像的模型类,我想将一些属性绑定到 XMAL-objects 属性。 Code in ChessBoardViewModel.cs: 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);
}

Chessboard is the name of the Grid containing all images. Chessboard 是包含所有图像的网格的名称。

My plan is that the image should be binded to each of the instances Row and Column values to determine where in the game they are.我的计划是将图像绑定到每个实例的 Row 和 Column 值,以确定它们在游戏中的位置。

Problem is that i cannot access the XAML objects for some reason even though i have included the Views namespace.问题是由于某种原因我无法访问 XAML 对象,即使我已经包含了 Views 命名空间。

Edit:编辑:

Ok this is my code and how im planning on doing this now:好的,这是我的代码以及我现在计划如何执行此操作:

<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>

and the Viewmodel containing a list of my ChessPieces like this和包含我的 ChessPieces 列表的 Viewmodel 像这样

ChessPieces.Add(new BlkRook() { Name = "BlkRook1", Row = 0, Column = 0 });

And i want to bind the XAML image Grid.Row and Grid.Column property's to the correct instance of my objects.我想将 XAML 图像 Grid.Row 和 Grid.Column 属性绑定到我的对象的正确实例。 But cant seem to find a way to achieve it.但似乎无法找到实现它的方法。

WPF is a data-centric language, or at least it works best when it is used in this way. WPF 是一种以数据为中心的语言,或者至少在以这种方式使用时效果最好。 What I mean by that is that we manipulate data objects , not UI objects .我的意思是我们操作数据对象,而不是UI 对象 So you should never need to access your UI 'board' control, instead Binding a collection to that control and manipulating the items in the collection .因此,您永远不需要访问您的 UI“板”控件,而是将集合Binding到该控件并操作集合中的项目

As a short example, you could Bind a collection of 64 items to a UniformGrid , with 32 of them having some field that makes them appear 'empty'.作为一个简短的示例,您可以将 64 个项目的集合BindUniformGrid ,其中 32 个项目具有一些使它们显示为“空”的字段。 For example, you could have a string property for each item that defines which image to display and the empty squares could have an empty string there.例如,您可以为每个项目设置一个string属性,用于定义要显示的图像,而空方块在那里可以有一个空string

Then instead of trying to move a UI item from one place in the Grid to another, you can simply move the relevant item in the collection into its new place... for example, if you wanted to move a piece up the board one square, then you'd move it 8 items towards the start of the collection and move the empty piece that was there 8 places towards the end.然后,与其尝试将 UI 项目从Grid一个位置移动到另一个位置,您只需将集合中的相关项目移动到其新位置……例如,如果您想将一块棋盘向上移动一格,然后您将它移向集合的开头 8 个项目,并将那里的空白部分移向结尾的 8 个位置。

In MVVM it is recommended that you use data binding to set and get data between your view (xaml) and your view-model (eg. ViewModel.cs class).在 MVVM 中,建议您使用数据绑定来设置和获取视图 (xaml) 和视图模型(例如 ViewModel.cs 类)之间的数据。

Here's an example:下面是一个例子:
Let's say you want to add a text block to your UI and access it's text property in the corresponding view-model class or set the text property in the UI from the view-model class.假设您想向 UI 添加一个文本块并在相应的视图模型类中访问它的文本属性,或者从视图模型类中设置 UI 中的文本属性。 So the way to achieve this is by binding the text property in the UI to an object in the view-model class.所以实现这一点的方法是将 UI 中的 text 属性绑定到 view-model 类中的一个对象。
Here is how you do it...这是你如何做到的...
In your xaml view:在您的 xaml 视图中:
<TextBlock text="{binding textValue}"/>

In your view-model class:在您的视图模型类中:
var textValue = do something to set Value of text1

What happens here is when the value of textValue changes in the view-model the text block's text property also changes it's value and the change is reflected in the UI.这里发生的情况是,当视图模型中textValue的值发生变化时,文本块的 text 属性也会更改它的值,并且该更改会反映在 UI 中。

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

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