【问题标题】:WPF XAML DataGrid Change Button Image Programmatically C#WPF XAML DataGrid 以编程方式更改按钮图像 C#
【发布时间】:2018-01-15 05:46:51
【问题描述】:

我有以下 xaml 代码:

        <DataGrid CanUserReorderColumns="False" 
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False" 
                  CanUserSortColumns="False"
                  SelectionMode="Extended" 
                  SelectionUnit="Cell"
                  Name="endpoints" 
                  AlternatingRowBackground="LightGray"
                  AlternationCount="2"
                  Margin="5"
                  Height="250"
                  ScrollViewer.CanContentScroll="True"
                  ScrollViewer.VerticalScrollBarVisibility="Visible"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled">

...

                    <DataGridTemplateColumn  Header="Validate" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button x:Name="validate" Click="validate_Click">
                                    <Image Source="{Binding validateImage}" Height="16" Width="16"/>
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

我使用 foreachloop 填充 DataGrid 的每一行:

endpoints.Items.Add(new endpointObjects() {... });

validate_Click 函数

    private void validate_Click(object sender, RoutedEventArgs e)
    {
        var currentRowIndex = endpoints.Items.IndexOf(endpoints.CurrentItem);
        var currentColIndex = endpoints.CurrentCell.Column.DisplayIndex;
        DataGridRow t = dataGridTools.GetRow(endpoints, currentRowIndex);
        var ip = (t.Item as endpointObjects).ip;
        var port = (t.Item as endpointObjects).port;

        MessageBox.Show("Validate Button Clicked: " + currentRowIndex + " | " + currentColIndex + " | " 
            + (t.Item as endpointObjects).isHost + " | " 
            + " | " + (t.Item as endpointObjects).ip
            + ":" + (t.Item as endpointObjects).port);
    }

数据网格视图

我似乎无法解决该图像。我希望有如下所示的 Javascript DOM 方法。

[datagrid] > [button name] > [image].Image Source = "images\newImage.png";
endpoints.edit.Image.Source = "images\newImage.png";

因此,当我单击按钮/图像时,它会触发一个函数(在本例中为 validate_Click),该函数执行特定任务并根据该任务的结果将按钮图像更改为另一个图像。

对所有这些都非常陌生,因此非常感谢任何帮助我了解我哪里出错的帮助。

更新 - 已解决

这已通过以下 2 行解决:

        var image =  (Image)(sender as Button).Content;
        image.Source = new BitmapImage(new Uri("images/tick.png", UriKind.RelativeOrAbsolute));

基本功能实现示例如下:

    private void validate_Click(object sender, RoutedEventArgs e)
    {
        var currentRowIndex = endpoints.Items.IndexOf(endpoints.CurrentItem);
        var currentColIndex = endpoints.CurrentCell.Column.DisplayIndex;
        DataGridRow t = dataGridTools.GetRow(endpoints, currentRowIndex);
        var ip = (t.Item as endpointObjects).ip;
        var port = (t.Item as endpointObjects).port;
        var image =  (Image)(sender as Button).Content;
        image.Source = new BitmapImage(new Uri("images/tick.png", UriKind.RelativeOrAbsolute));
        MessageBox.Show("Validate Button Clicked: " + currentRowIndex + " | " + currentColIndex + " | "
            + (t.Item as endpointObjects).isHost + " | "
            + (t.Item as endpointObjects).FTRname
            + " | " + (t.Item as endpointObjects).ip
            + ":" + (t.Item as endpointObjects).port
            + " | " + image.Source.ToString()
            );
    }

【问题讨论】:

  • 你可以使用转换器来做到这一点。检查以下示例代码。

标签: c# wpf xaml datagrid


【解决方案1】:

validate_Click方法中,sender是用户点击的按钮。因此,您可以通过转换按钮内容来获得 Image 控件。

var image = (Image)((sender as Button).Content);

然后设置ImageSource:

image.Source = new BitmapImage(new Uri(...));

【讨论】:

  • 这正是我所追求的,您所描述的很有意义,因为它是按钮的子项。我将更新问题中的最终代码。谢谢!
  • 很高兴能帮上忙。
  • 这是错误的方法。设置 image.Source 会替换之前分配的绑定 Source="{Binding validateImage}",并且忽略 validateImage 的任何后续更改。而不是设置 image.Source,而是更改 validateImage.
【解决方案2】:

试试这个代码,

public class ImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                string code = value as string;
                if (!string.IsNullOrEmpty(code))
                    return string.Format("/pathof the image/{0}.png", code);
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

【讨论】:

  • 鉴于问题中的详细信息,您能否向我解释一下我将如何实现此类,我似乎无法访问图像元素来设置 Source 标签。也许我错过了什么?
  • 如果你有耐心可以阅读这篇文档如何使用值转换器wpftutorial.net/ValueConverters.html
  • 感谢您的链接,但是转换对象不是我遇到的问题。它正在解决我遇到问题的按钮标签中保存的图像标签Source 参数。正如您在我的代码中看到的,我已经在 validate_Click 函数 (t.Item as endpointObjects).isHost 中进行了对象转换 - 我需要在 DataGrid > Button > Image 中处理 Source 标记。对不起,如果我没有说清楚。
  • 在 wpf 中,您可以将网格单元格转换为按钮类型。所以我们可以实现这个方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 2011-08-23
  • 1970-01-01
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多