【问题标题】:Databinding Grid position - wpf数据绑定网格位置 - wpf
【发布时间】:2016-04-19 10:32:08
【问题描述】:

我有一个网格,我动态构建它,并在构建时将图像放在网格的第一个单元格 (0, 0) 中。

我想使用数据绑定,这样当用户改变位置时,图像会改变它的位置到网格中的正确位置。

用户有 2 个 texbox,他将行和列放在网格中。

任何帮助将不胜感激。

编辑: 到目前为止我所做的代码: 看法: 我有一个包含此用户控制器的窗口

public MyController()
    {
        InitializeComponent();
        this.playerImage = new Image();
    }

    public void CreateGrid(string ansFromServer, int numberOfRows, int numberOfCols, int type)
    {
        this.rows = numberOfRows;
        this.cols = numberOfCols;
        for (int i = 0; i < this.rows; i++)
        {
            RowDefinition def = new RowDefinition();
            mainGrid.RowDefinitions.Add(def);
        }
        for (int i = 0; i < this.cols; i++)
        {
            ColumnDefinition def = new ColumnDefinition();
            mainGrid.ColumnDefinitions.Add(def);
        }
}

【问题讨论】:

  • 发布你的一些代码,展示你到目前为止所做的尝试
  • “当用户改变位置”是什么意思?用户做什么?
  • 用户将行和列插入到2个文本框
  • 将 Image 的 Grid.ColumnGrid.Row 属性绑定到视图模型中的两个 int 属性,例如 Grid.Column="{Binding GridColumn}"
  • Clemens,你能告诉我如何在后面的代码中做到这一点吗?

标签: c# wpf grid


【解决方案1】:

为列和行创建一个具有两个整数属性的简单视图模型:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int gridColumn;
    public int GridColumn
    {
        get { return gridColumn; }
        set { gridColumn = value; OnPropertyChanged(); }
    }

    private int gridRow;
    public int GridRow
    {
        get { return gridRow; }
        set { gridRow = value; OnPropertyChanged(); }
    }

    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

并在 XAML 中使用它,例如像这样:

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal">
        <TextBox Width="50"
                 Text="{Binding GridColumn, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox Width="50"
                 Text="{Binding GridRow, UpdateSourceTrigger=PropertyChanged}"/>
    </StackPanel>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Image Source="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
               Grid.Column="{Binding GridColumn}" Grid.Row="{Binding GridRow}"/>
    </Grid>
</Grid>

如果您在后面的代码中创建了 Image 控件,您可以像这样设置绑定:

playerImage.SetBinding(Grid.ColumnProperty, new Binding("GridColumn"));
playerImage.SetBinding(Grid.RowProperty, new Binding("GridRow"));
mainGrid.Children.Add(playerImage);

【讨论】:

  • 非常非常感谢 :) 我真的很感谢它!但是我如何在后面的代码中进行绑定呢?我在后面的代码中创建网格:/
  • @DvirSamuel 您应该编辑您的问题并向我们展示您到目前为止所做的事情。这样会更容易回答您的问题。
  • 我要绑定playerImage
  • 看看我的帖子底部。
猜你喜欢
  • 2011-09-28
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 2012-07-03
  • 2013-11-14
相关资源
最近更新 更多