WPF XAML DataGrid以编程方式更改按钮图像C#
我有以下xaml代码:WPF XAML DataGrid以编程方式更改按钮图像C#,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有以下xaml代码: <DataGrid CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" SelectionMode="Extended"
<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">
验证单击功能
数据网格视图
我只是似乎无法解决这个问题。我希望得到如下所示的Javascript DOM方法
[datagrid] > [button name] > [image].Image Source = "images\newImage.png";
endpoints.edit.Image.Source = "images\newImage.png";
因此,当我单击按钮/图像时,它会触发一个函数(在本例中为validate_click),该函数执行特定任务,并根据该任务的结果将按钮图像更改为另一个图像
这一切都是全新的,所以任何帮助我了解我哪里出了问题的人都将不胜感激
更新-已解决
通过以下两行解决了此问题:
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()
);
}
试试这个代码
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;
}
}
在
validate\u Click
方法中,sender
是用户单击的按钮。因此,您可以通过强制转换按钮内容来获得图像控件
var image = (Image)((sender as Button).Content);
然后设置图像源:
image.Source = new BitmapImage(new Uri(...));
您可以使用转换器来完成此操作。检查下面的示例代码。考虑到问题中的细节,您能否向我解释一下如何实现这个类,我似乎无法访问image元素来设置源标记。也许我遗漏了什么?如果您有耐心,您可以阅读本文档如何使用值转换器感谢链接,但是转换对象不是我的问题。它正在寻址按钮标记中的image标记
Source
参数,我遇到了麻烦。正如您在我的代码中所看到的,我已经在validate_Click函数中进行了对象转换(t.Item作为endpointObjects)。iHost
-我需要在DataGrid>按钮>图像中处理源标记。对不起,我不清楚。在wpf中,您可以将网格单元格转换为按钮类型。所以我们可以实现这个方法,这正是我想要的,你所描述的是有意义的,因为它是按钮的孩子。我将更新问题中的最终代码。非常感谢。我很高兴能帮上忙。这是错误的方法。设置image.Source
将替换以前分配的绑定,Source=“{Binding validateImage}”
,并忽略validateImage的任何后续更改。更改validateImage,而不是设置image.Source。
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;
}
}
var image = (Image)((sender as Button).Content);
image.Source = new BitmapImage(new Uri(...));