SL中如何判断控件内的某点坐标的颜色? 比如我的Grid, 我想知道在0,0是什么颜色, 0,1是什么颜色?

muzizongheng
博客专家认证
2010-03-12 09:56:30
如题。
我现在想在 Grid控件的格子上画个字,

但不知道哪个格子上有字的一部分, 比如我的grid的背景是黑色, 字是红色, 我想知道grid内的每个cell的颜色 是黑色 还是红色?
...全文
103 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
muzizongheng 2010-03-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jv9 的回复:]

上面代码需要使用VisualTreeHelper,详细可以看看这篇文章。

http://silverlightchina.net/html/developer/2010/0312/834.html
[/Quote]

谢谢你啊, 呵呵, 我刚学SL, 就是觉得没有WPF的 visualTree 很不方便。
jv9 2010-03-13
  • 打赏
  • 举报
回复
上面代码需要使用VisualTreeHelper,详细可以看看这篇文章。

http://silverlightchina.net/html/developer/2010/0312/834.html
jv9 2010-03-13
  • 打赏
  • 举报
回复
Grid控制Cell比较麻烦,尽管实现效率也不高。可以考虑使用自定义控件控制,或者使用datagrid控件。

相对来说,控制Grid的Row比较容易,你可以参考下面的代码,设置Grid的Row背景。


private List<DataGridRow> _myDataGridRows = new List<DataGridRow>();

void myDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
_myDataGridRows.Add(e.Row);
}

void myDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.ResetRowsColor();

var possibleSelection = _myDataGridRows.Where(w => w.GetIndex() == myDataGrid.SelectedIndex - 1 || w.GetIndex() == myDataGrid.SelectedIndex + 2)
.ToList();

if (possibleSelection.Count == 2)
{
this.SetRowColor(possibleSelection.ElementAt(0), Colors.Red);
this.SetRowColor(possibleSelection.ElementAt(1), Colors.Blue);
}

else if (possibleSelection.Count == 1)
{
if (possibleSelection.ElementAt(0).GetIndex() < myDataGrid.SelectedIndex)
{
this.SetRowColor(possibleSelection.ElementAt(0), Colors.Red);
}
else
{
this.SetRowColor(possibleSelection.ElementAt(0), Colors.Blue);
}
}
}

public void ResetRowsColor()
{
foreach (var row in this._myDataGridRows)
{
this.SetRowColor(row, Colors.Transparent);
}
}

private void SetRowColor(DataGridRow row, Color color)
{
Grid rootGrid = MyVisualTreeHelper.SearchFrameworkElement(row, "Root") as Grid;
if (rootGrid != null)
{
rootGrid.Background = new SolidColorBrush(color);

}
}


public FrameworkElement SearchFrameworkElement(FrameworkElement parentFrameworkElement, string childFrameworkElementNameToSearch)
{
FrameworkElement childFrameworkElementFound = null;
SearchFrameworkElement(parentFrameworkElement, ref childFrameworkElementFound, childFrameworkElementNameToSearch);
return childFrameworkElementFound;
}
private void SearchFrameworkElement(FrameworkElement parentFrameworkElement, ref FrameworkElement childFrameworkElementToFind, string childFrameworkElementName)
{
int childrenCount = VisualTreeHelper.GetChildrenCount(parentFrameworkElement);
if (childrenCount > 0)
{
FrameworkElement childFrameworkElement = null;
for (int i = 0; i < childrenCount; i++)
{
childFrameworkElement = (FrameworkElement)VisualTreeHelper.GetChild(parentFrameworkElement, i);
if (childFrameworkElement != null && childFrameworkElement.Name.Equals(childFrameworkElementName))
{
childFrameworkElementToFind = childFrameworkElement;
return;
}

SearchFrameworkElement(childFrameworkElement, ref childFrameworkElementToFind, childFrameworkElementName);
}
}
}
muzizongheng 2010-03-12
  • 打赏
  • 举报
回复
cell?? 如何得来的。 请明示
muzizongheng 2010-03-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiamiking 的回复:]
额。。这样有可能不行。

可以先做两个Brush

C# code
Brush redBrush=new SolidColorBrush(Colors.Red);

Brush blackBrush=new SolidColorBrush(Colors.Black);

grid.BackGroud=blackBrush;

然后就可以用cell.BackGround==blackBrush来判断
[/Quote]

ok, 我去试试, 可以的话 立马给分。 呵呵
xiamiking 2010-03-12
  • 打赏
  • 举报
回复
额。。这样有可能不行。

可以先做两个Brush


Brush redBrush = new SolidColorBrush(Colors.Red);

Brush blackBrush = new SolidColorBrush(Colors.Black);

grid.BackGroud=blackBrush;


然后就可以用cell.BackGround==blackBrush来判断
xiamiking 2010-03-12
  • 打赏
  • 举报
回复

if (cell.Background == new SolidColorBrush(Colors.Red)) { };


判断是否为红色的,判断黑色把Colors.Red换成Colors.Black就好了
muzizongheng 2010-03-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 goldfire_001 的回复:]
Grid只是用来支持LayOut用的,他本身并不是一个逻辑划分Child的Control。

所以,我的建议是遍历Grid的Children。然后检查每个Children的Grid.Row和Grid.Column。找到匹配的Control后,进行Brush比较。

另外,1楼的回答请慎重考虑。我怎么觉得
new SolidColorBrush(Colors.Red)==new SolidColorBrush(Colors.Red);这样的Code肯定会返回false呢?
SolidColorBrush可是Class啊,Object比较不能用==吧。

[/Quote]

呵呵, 可是每次我要操作cell时 都要遍历grid所有的cell, 效率太低啊。
Goldfire_001 2010-03-12
  • 打赏
  • 举报
回复
Grid只是用来支持LayOut用的,他本身并不是一个逻辑划分Child的Control。

所以,我的建议是遍历Grid的Children。然后检查每个Children的Grid.Row和Grid.Column。找到匹配的Control后,进行Brush比较。

另外,1楼的回答请慎重考虑。我怎么觉得
new SolidColorBrush(Colors.Red)==new SolidColorBrush(Colors.Red);这样的Code肯定会返回false呢?
SolidColorBrush可是Class啊,Object比较不能用==吧。

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧