Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 views
in Technique[技术] by (71.8m points)

.net - XAML syntax error is blocking designer but compiling fine

I've run into a rather bizarre thing happening, I have a DataGrid defined in a WPF XMAL page that has the following declared:

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF3399FF" />
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FF3399FF"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" />
</DataGrid.Resources>

Technically the two Inactive SystemColors types are from .net 4.5, however I can compile the program when it's set to target .net 4, and these Inactive brushes work, but while it's set to target .net 4 loading the XMAL designer page in Visual Studio throws the error The member "InactiveSelectionHighlightTextBrushKey" is not recognized or is not accessible. and then blocks the designer view. But it still compiles and displays as defined above in the program.

This seems very inconsistent to say the least and I can’t tell if this is a Visual Studio 2012 issue or if it’s allowing the program to compile because my development computer has .net 4.5 installed and it’s just changing the target framework when it sees that something is using it (I highly doubt this though). Or is it possible that the Inactive types are in .net 4 but not listed as supported in the documentation and just causing this issue in VS?

Is there a better way to do this in .net 4 to allow me to set the inactive selection color of a DataGrid row? Or is the only way to do this being to upgrade to .net 4.5?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Visual Studio builds an assembly, even if its target set to .NET FW 4.0 and you use InactiveSelectionHighlightBrushKey in XAML-code. This assembly will be correctly performed in a system with .NET FW 4.5. But if the system has only .NET FW 4.0, an exception will be thrown when the system create User control with InactiveSelectionHighlightBrushKey.

So you cannot use the InactiveSelectionHighlightBrushKey in assemblies with target set to FW 4.0, because they will not work in a system with only .NET FW 4.0.

To support both FW 4.0 and FW 4.5 you can set a color of the selected row in handlers of LostFocus/LostKeyboardFocus/GotFocus events. See the example code https://stackoverflow.com/a/8095932/1815957


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...