我无法使用数据绑定ItemContainerStyle了ListBox在Silverlight 3.它工作正常WPF.这是举例说明我的问题.我真正想要的是绑定到IsSelected属性,但我认为这个例子更容易遵循.
我有一个ListBox绑定到ObservableCollection<Item>的Item对象:
public class Item {
public String Name { get; }
public Brush Color { get; }
}
Run Code Online (Sandbox Code Playgroud)
这是相关的Silverlight XAML:
<ListBox x:Name="listBox" ItemsSource="{Binding .}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="{Binding Color}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
如果TargetType="ListBoxItem"替换为WPF,则可以在WPF中使用相同的XAML TargetType="{x:Type ListBoxItem}".
WPF应用程序将在列表框中显示项目,并根据对象的Color属性设置其背景颜色Item.但是,Silverlight应用程序XamlParseException因文本失败而失败AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR.作为一个顽固的家伙我甚至试图删除失败的XAML并创建我自己的风格,而不是这样:
Binding binding = new Binding("Color");
Setter setter = new Setter(ListBoxItem.BackgroundProperty, …Run Code Online (Sandbox Code Playgroud) 我想为UserControl可以包含UIElement对象集合的依赖项属性添加.您可能会建议我从中获取控件Panel并使用该Children属性,但在我的情况下它不是一个合适的解决方案.
我修改过UserControl这样的:
public partial class SilverlightControl1 : UserControl {
public static readonly DependencyProperty ControlsProperty
= DependencyProperty.Register(
"Controls",
typeof(UIElementCollection),
typeof(SilverlightControl1),
null
);
public UIElementCollection Controls {
get {
return (UIElementCollection) GetValue(ControlsProperty);
}
set {
SetValue(ControlsProperty, value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
而我正在使用它:
<local:SilverlightControl1>
<local:SilverlightControl1.Controls>
<Button Content="A"/>
<Button Content="B"/>
</local:SilverlightControl1.Controls>
</local:SilverlightControl1>
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我运行应用程序时出现以下错误:
Object of type 'System.Windows.Controls.Button' cannot be converted to type
'System.Windows.Controls.UIElementCollection'.
Run Code Online (Sandbox Code Playgroud)
在" 使用集合语法设置属性"部分中,明确声明:
[...]您无法在XAML中明确指定[UIElementCollection],因为UIElementCollection不是可构造的类.
我该怎么做才能解决我的问题?解决方案只是使用另一个集合类而不是UIElementCollection?如果是,建议使用的集合类是什么?
为什么java.lang.Thread在谷歌App Engine的白名单中时,不支持呢?
我正在开发键盘控制,非常简单地嵌入在表单上.使用sendkey类来执行char条目.要使这个功能需要知道以前选择的控件.
我正在使用Silverlight VisualStateManager.GoToState方法将我的控件从一个状态转换到另一个状态.这一切都很好,过渡动画效果很好.但我想知道转换何时完成,以便我可以在我的代码中启动其他操作.是否有事件或其他机制可以用来发现何时完成向另一个州的过渡?
我在我的WPF格式中使用了一个DatePicker,并且遇到的问题是,当用户输入英国格式的日期时,即31/12/1980,WPF似乎想将其转换为美国,所以它读取为12/31/1980年.
这让我在SQL插入中超出了范围异常.
有没有人遇到过这个问题,如果有的话,你是如何解决的?
我希望能够在屏幕上的某个点确定winform的背景颜色,因此我可以根据特定的颜色采取一些措施.遗憾的是,System.Drawing库似乎没有指定一个允许我这样做的方法.
那么我该如何确定某一点的颜色呢?
我目前正在对Windows服务进行维护,并且在代码中的某些点处存在一些异常处理(例如,来自计时器和其他外部事件的回调):
try {
...
}
catch (Exception ex) {
_logger.Log("Unhandled exception: {0}\r\n{1}", ex.Message, ex.StackTrace);
Environment.FailFast("Fatal error.");
}
Run Code Online (Sandbox Code Playgroud)
记录异常信息有助于排除出错的问题.但是,有时候有趣的信息是内部异常,这使得很难确定问题的根本原因.例如,a TypeInitializationException很难理解.
是否有更好的方法来记录异常信息以进行故障排除?
我正在尝试从C#调用非托管C ++,但是收到有关返回值的异常。例外:
'System.Runtime.InteropServices.MarshalDirectiveException:'无法编组'返回值':无效的托管/非托管类型组合(只能将数组编组为LPArray,ByValArray或SafeArray)。
我有一个类似的函数,看起来很一样,没有返回值(void),可以正常工作而没有任何问题。
我将c ++项目的平台(编译器)设置为v100(Visual Studio 2010),并在c#项目中使用.net 4.5。
C ++项目创建了一个lib + dll文件,我将它们都放置在可执行文件夹中。
当我尝试在C#代码处将返回值替换为“ String”时,异常转换为:
System.AccessViolationException:'试图读取或写入受保护的内存。这通常表明其他内存已损坏。
删除返回值函数属性([return: MarshalAs(UnmanagedType.BStr)])时,收到以下异常:
'System.Runtime.InteropServices.MarshalDirectiveException:'无法编组'返回值':无效的托管/非托管类型组合。'
当我进行以下组合时:删除返回值函数属性并将返回类型转换为字符串,应用程序将关闭自身而不会捕获任何异常。
C ++代码
extern "C"
{
ExternalDll_API char* FuncA(char* projectId);
}
ExternalDll_API char* FuncA(char* projectId)
{
return "abc";
}
Run Code Online (Sandbox Code Playgroud)
C#代码
[DllImport("ExternalDll.dll")]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern char[] FuncA(string projectId);
var key = FuncA(projectId.ToString());
Run Code Online (Sandbox Code Playgroud) 我正在使用 C# 和 LINQPad。如何可视化图表和树?
例如,如果我创建一个像这样的树数据结构:
public class BinaryTree<T>()
{
public T Value { get; set; }
public BinaryTree<T> LeftChild { get; set; }
public BinaryTree<T> RightChild { get; set; }
//Other methods:
}
Run Code Online (Sandbox Code Playgroud)
我想要一种简单的方法来在执行程序时显示它,例如,如下所示:
c# ×6
.net ×4
silverlight ×3
winforms ×2
2d ×1
c#-4.0 ×1
c++ ×1
com ×1
data-binding ×1
exception ×1
graphics ×1
java ×1
linqpad ×1
listbox ×1
logging ×1
marshalling ×1
wpf ×1
wpf-controls ×1
xaml ×1