我有一个DataGrid,这个DataGrid的一部分是几个带有切换选项的列。这些可切换选项显示为图像,如果选项为“真”,则为彩色,如果选项为“假”,则显示为灰色。有一个MouseDown事件附加到每个图像,行中有一个转换器,结构最终如下所示:
当前结构:
...
<DataGridTemplateColumn Header="" Width="20" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Converter={StaticResource Option1Converter}}" MouseDown="Option1_MouseDown" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="" Width="20" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Converter={StaticResource Option2Converter}}" MouseDown="Option2_MouseDown"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="" Width="20" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Converter={StaticResource Option3Converter}}" MouseDown="Option3_MouseDown" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...
MouseDown事件的处理方式如下。我只需找到行中的主键即可轻松识别行。我不知道该怎么做,实际上是在这一行和一列切换图像。我希望能够在此行/列中说出Image.Source = thisImage&#39;,但我不确定如何实现这一目标。 MouseDown事件在这里:
private void Option1_MouseDown(object sender, MouseEventArgs e)
{
var dc = (sender as System.Windows.Controls.Image).DataContext;
DataRowView row = (DataRowView) dc;
String URL = Convert.ToString(row.Row["ID"]);
int newState = myQuery.UpdateIsTogglable(rowID);
if (newState)
// Toggle the image at this row. <-- This is the end goal!
else
// Toggle the image at this row.
}
实际值是DB中的int。转换器在加载Datagrid时处理实际的初始状态:
public class PostIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ImageSource result = null;
//var intValue = (int)value;
DataRowView rawRows = (DataRowView) value;
DataRow row = (DataRow) rawRows.Row;
int intValue = row.Field<Int32>("Option1");
switch (intValue)
{
case 0:
{
result = new BitmapImage(new Uri(@"/myProject;component/Images/Option1_false.png", UriKind.Relative));
break;
}
case 1:
{
result = new BitmapImage(new Uri(@"/myProject;component/Images/Option1_true.png", UriKind.Relative));
break;
}
}
return result;
}
}
现在,我试图为行分配一个int值,但转换器并没有按照我希望的方式实际翻转它。这是我的非工作尝试:
// This doesn't work :(
private void Option1_MouseDown(object sender, MouseEventArgs e)
{
var dc = (sender as System.Windows.Controls.Image).DataContext;
DataRowView row = (DataRowView) dc;
String URL = Convert.ToString(row.Row["ID"]);
int newState = myQuery.UpdateIsTogglable(rowID);
if (newState)
row.Row["Option1"] = 1;
else
row.Row["Option1"] = 0;
}
目标:
我需要能够在点击时切换这些图标的图像源。我点击它时不确定从数据网格中获取特定图像控制需要做什么。有没有简单的方法来做到这一点?我可以通过列索引或名称以某种方式实现此目的吗?
答案 0 :(得分:0)
我想出了一个解决方案:
private void Option1_MouseDown(object sender, MouseEventArgs e)
{
var dc = (sender as Image).DataContext;
DataRowView row = (DataRowView) dc;
String URL = Convert.ToString(row.Row["ID"]);
int newState = myQuery.UpdateIsTogglable(rowID);
if (newState)
(sender as Image).Source =
new BitmapImage(new Uri(@"/myProject;component/Images/Option1_true.png", UriKind.Relative));
else
(sender as Image).Source =
new BitmapImage(new Uri(@"/myProject;component/Images/Option1_false.png", UriKind.Relative));
}
我不确定这是否是实现这一目标的理想方式,但它易于理解和实施!