小编PaN*_*1Me的帖子

通用类型与抽象类/接口

假设我们在.NET中创建一个通用控件.比如一棵树.我不明白为什么人们使用这种泛型类型定义

Control<T>
Run Code Online (Sandbox Code Playgroud)

在面向对象编程时我可以使用抽象类或接口:

Control<IItem> or Control<BaseClass>
Run Code Online (Sandbox Code Playgroud)

所以唯一要做的就是,它们的类型必须从该基类派生或实现接口.这是否意味着,泛型类型更方便,因为您不必实现或继承任何东西?

generics abstract-class interface

7
推荐指数
1
解决办法
6220
查看次数

捕获.NET线程中的异常

你怎么能找到,一个Exception在发生Thread多线程应用程序?并连续清理资源?

因为否则Thread仍然可以保留在内存中并运行.

.net c# multithreading

7
推荐指数
1
解决办法
951
查看次数

WPF ListView始终关注最后添加的项目?

我有许多具有相同DataSource的ListView,IsSynchronizedWithCurrentItem="True" 我正在动态地向这个DataSource添加项目.

问题是当滚动出现时,除非我移动滚动条,否则添加的项目不可见.我应该为此目的使用另一个控件..或者如何将最后添加的项目带入视图(和滚动条).

直到现在我才直接在XAML中做所有事情,所以如果可能的话,我会很感激这样的解决方案.

wpf listview

6
推荐指数
1
解决办法
6560
查看次数

C#如何检查类是否实现了通用接口?

如何获取实例的通用接口类型?

假设这段代码:

interface IMyInterface<T>
{
    T MyProperty { get; set; }
}
class MyClass : IMyInterface<int> 
{
    #region IMyInterface<T> Members
    public int MyProperty
    {
        get;
        set;
    }
    #endregion
}


MyClass myClass = new MyClass();

/* returns the interface */
Type[] myinterfaces = myClass.GetType().GetInterfaces();

/* returns null */
Type myinterface = myClass.GetType().GetInterface(typeof(IMyInterface<int>).FullName);
Run Code Online (Sandbox Code Playgroud)

c# generics types interface

6
推荐指数
1
解决办法
4046
查看次数

WPF触发器不为空

如何在属性不为null时在WPF中触发操作?当为null时,这是一个有效的解决方案:

<Style.Triggers>
    <DataTrigger Binding="{Binding}" Value="{x:Null}">

      <Setter Property="Background" Value="Yellow" />

    </DataTrigger>
</Style.Triggers>
Run Code Online (Sandbox Code Playgroud)

我知道你不能"扭转"这种情况并做你需要的事情,但想知道

wpf triggers

5
推荐指数
2
解决办法
2万
查看次数

WPF自定义控件样式问题

我有一个自定义控件(来自MS Toolkit - DatePicker).我做了这样的风格:

<Style TargetType="{x:Type local:DatePicker}">
Run Code Online (Sandbox Code Playgroud)

但这种风格不会自动应用.我必须添加密钥:

<Style x:Key="DatePickerStyle" TargetType="{x:Type local:DatePicker}">
Run Code Online (Sandbox Code Playgroud)

并在每个自定义控件中引用它

<toolkit:DatePicker Style="{StaticResource DatePickerStyle}"
Run Code Online (Sandbox Code Playgroud)

...

让它工作.有谁知道为什么?

wpf controls styles

4
推荐指数
1
解决办法
9376
查看次数

WPF TextBox丑陋的边框问题

我想覆盖WPF中的默认TextBox边框.我有这种风格适用于所有TextBoxes.

<!-- StyleTextBox-->
<Style x:Key="StyleTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="MinHeight" Value="20" />
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Margin" Value="3"/>
    <Setter Property="IsEnabled" Value="{DynamicResource WriteAble}"/>
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="{StaticResource ButtonFont_DarkGray}" />
    <Style.Triggers>
        <!--Resolves multiline textbox vertical alignment problem-->
        <Trigger Property="TextWrapping" Value="NoWrap">
            <Setter Property="VerticalContentAlignment" Value="Center" />
        </Trigger>
    </Style.Triggers>
</Style> 
Run Code Online (Sandbox Code Playgroud)

我添加SnapsToDevicePixels="True"了在LCD显示器上正确显示边框.

但是,每个TextBox似乎都不同.一些边界丢失或灰色..有谁知道为什么?

wpf textbox border

4
推荐指数
1
解决办法
1万
查看次数

WPF ErrorTemplate在没有聚焦时可见?

我正在使用WPF验证进行TextBox验证.我已经定义了这个模板:

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}" BasedOn="{StaticResource StyleTextBox}">        
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                <Setter Property="Background" Value="{StaticResource TextBox_ErrorBackgroundBrush}"/>
                <Setter Property="BorderBrush" Value="{StaticResource TextBox_ErrorBorderBrush}"/>
                <Setter Property="BorderThickness" Value="2"/>                      
            </Trigger>
        </Style.Triggers>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel>
                        <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="20" Text="!"/>
                        <AdornedElementPlaceholder/>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>  
Run Code Online (Sandbox Code Playgroud)

TextBox位于TabItem中的表单上.一切正常,但'!' 当我选择其他TabItem时,TextBlock保持可见.在许多其他情况下会观察到这种行为 - 当扩展器扩展等时.虽然TextBox没有显示,但Excklamation始终在同一个地方保持可见.

validation wpf templates focus

4
推荐指数
1
解决办法
2161
查看次数

WPF ICollectionView过滤

我编写了一个用于在ComboBox中过滤项目的代码:

我的问题是,你会怎么做?

我认为这种带反射的解决方案可能非常 ..

ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
view.Filter += this.FilterPredicate;


private bool FilterPredicate(object value)
{            
    if (value == null)
        return false;

    if (String.IsNullOrEmpty(SearchedText))
        return true;            

    int index = value.ToString().IndexOf(
        SearchedText,
        0,
        StringComparison.InvariantCultureIgnoreCase);

    if ( index > -1) return true;

    return FindInProperties(new string[] { "Property1", "Property2" }, value, SearchedText);
}

private bool FindInProperties(string[] properties, object value, string txtToFind)
{
    PropertyInfo info = null;
    for (int i = 0; i < properties.Length; i++)
    {
        info = value.GetType().GetProperty(properties[i]);
        if …
Run Code Online (Sandbox Code Playgroud)

wpf performance filtering filter icollectionview

4
推荐指数
1
解决办法
2万
查看次数

.NET返回值与抛出异常设计问题

假设我们有一个方法处理位于处理这种结构的类中的树分层数据结构中的操作.

让我们仔细看看其中一种方法:

void MoveNode(Node currentNode, Node newParentNode)
{
    /* check if new parent isn't current's child already */
     if (newParentNode.LMargin < currentNode.LMargin && newParentNode.RMargin > currentNode.RMargin)
     {
        //DO WORK
     }
     else throw new ArgumentException("New parent node cannot be current's own child");
}
Run Code Online (Sandbox Code Playgroud)

MSDN声明:不要抛出异常来控制流量!

我的问题:在您看来,这种ArgumentException的使用是否正常,或者您是否会使用某种返回值.如果是这样,您将如何提供错误/验证消息.

.net c# exception-handling return-value

4
推荐指数
1
解决办法
3381
查看次数