我的下面的例子涉及2个.NET类,它们都包含CommonMethod方法.我想设计MyMethod,可以接受任何类(使用),同时保留NetClassA和NetClassB常用的功能.Case1会做到这一点,只有它是非法的,如下所述.Case2也将完成目标,除了INetClassA和INetClassB不存在.因此,我的问题是有一种方法可以在现有的.NET类型(案例3)上强制使用自定义接口(ICommonNetMethods)吗?我的问题的替代解决方案受到欢迎.
// Case 1: Illegal because "where" can only have 1 base class
public void MyMethod<Ttype>(Ttype myClass) where Ttype : NetClassA, NetClassB {}
// Case 2: Legal to utlize multiple "where" interface types
public void MyMethod<Ttype>(Ttype myClass) where Ttype : INetClassA, INetClassB {}
// Case 3: For this to work ICommonNetMethods must be added to NetClassA/NetClassB
public void MyMethod<Ttype>(Ttype myClass) where Ttype : ICommonNetMethods {}
NetClassA() { This .NET class has method CommonMethod() }
NetClassB() { This .NET class has …Run Code Online (Sandbox Code Playgroud) 编辑:我创建了一个新的VS2010 WPF应用程序,只有3个文件MainWindow.xaml,MainWindow.xaml.cs和MainWindowViewModel.cs(下面列出).如果有人觉得非常有帮助,您可以在几秒钟内重新创建问题(复制/粘贴).当您运行应用程序时,DataGrid将显示字符串"OldCollection",这是错误的.如果将ItemsSource绑定更改为MyCollection,则显示"NewCollection",这是正确的.
完整描述: 起初我有一个DataGrid,其ItemsSource绑定到MyCollection.我需要一个方法UpdateCollection,它将一个新的ObservableCollection <>分配给MyCollection.通过向MyCollection添加NotifyPropertyChange,UI更新.
接下来,有必要引入一个CollectionViewSource来启用分组.将UI绑定到MyCollectionView后,对UpdateCollection的调用现在无效.调试器确认MyCollectionView始终包含初始MyCollection.如何让我的NewCollection反映在View中?我尝试过View.Refresh(),绑定CollectionViewSource,以及无数其他策略.
注意:其他人主要关注收集项目的更改而不更新视图(分组/排序)而不调用Refresh.我的问题是我正在为CollectionViewSource分配一个全新的集合,并且视图/ UI永远不会改变.
// MainWindow.xaml
<Window x:Class="CollectionView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="grid" ItemsSource="{Binding MyCollectionView}" />
</Grid>
</Window>
//MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace CollectionView
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
}
//MainWindowViewModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel; …Run Code Online (Sandbox Code Playgroud) 我希望能够删除FlowDocument的各种内联元素之间的空白.下面是一个非常具体的例子,只是为了解决问题.所需的输出是"Hello World?" 但是会发生什么是"Hello World?".在这种情况下 "?" 是一个可点击的按钮.
我搜索了一段时间没有成功.我尝试了所有形式的打击垫/边距调整,但它们只能增加间距.我开始相信这个空间是FlowDocument元素边界所固有的.这似乎是一系列限制.
<RichTextBox>
<FlowDocument>
<Paragraph Margin="0">
<Run>
Hello World
</Run>
<InlineUIContainer>
<Button Click="ButtonClick">?</Button>
</InlineUIContainer>
</Paragraph>
</FlowDocument>
</RichTextBox>
Run Code Online (Sandbox Code Playgroud) 我有一个具有动态参数的方法并返回动态结果.我希望能够将null,int,string等传递给我的方法.但是我在所有情况下都得到"NotSupportedException".
MyMethod(null); // Causes problems (Should resolve to ref type?)
MyMethod(0); // Causes problems (Should resolve to int type)
public dynamic MyMethod(dynamic b)
{
if (value != null) {...}// Throws NotSupportedExpception
if (value != 0) {...} // Throws NotSupportedExpception
}
Run Code Online (Sandbox Code Playgroud)