小编Mar*_*ius的帖子

wpf:如何通过命令禁用按钮时显示工具提示?

无论按钮状态如何,我都试图显示工具提示,但这似乎没有办法:

<Button Command="{Binding Path=CommandExecuteAction}" 
        ToolTip="{Binding Path=Description}" ToolTipService.ShowOnDisabled="true"
        Style="{StaticResource toolbarButton}">
   <Image Source="{Binding Path=Icon}"></Image>
</Button>
Run Code Online (Sandbox Code Playgroud)

如果由于command.CanExecute返回false而禁用按钮时如何显示工具提示?

注意:

ToolTipService.ShowOnDisabled ="true"就像一个魅力.这在我的示例中不起作用的原因是因为与按钮关联的样式重新定义了控件模板,并在禁用按钮时关闭按钮上的命中测试(IsHitTestVisible = false).重新启用controltemplate中的命中测试,使按钮被禁用时显示工具提示.

wpf

150
推荐指数
3
解决办法
3万
查看次数

从DataTemplate访问父DataContext

我有一个ListBox绑定到ViewModel上的子集合.列表框项基于父ViewModel上的属性在datatemplate中设置样式:

<Style x:Key="curveSpeedNonConstantParameterCell">
   <Style.Triggers>
      <DataTrigger Binding="{Binding Path=DataContext.CurveSpeedMustBeSpecified, 
          ElementName=someParentElementWithReferenceToRootDataContext}" 
          Value="True">
          <Setter Property="Control.Visibility" Value="Hidden"></Setter>
      </DataTrigger>
   </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

我收到以下输出错误:

System.Windows.Data Error: 39 : BindingExpression path error: 
 'CurveSpeedMustBeSpecified' property not found on 
   'object' ''BindingListCollectionView' (HashCode=20467555)'. 
 BindingExpression:Path=DataContext.CurveSpeedMustBeSpecified; 
 DataItem='Grid' (Name='nonConstantCurveParametersGrid');
 target element is 'TextBox' (Name=''); 
 target property is 'NoTarget' (type 'Object')
Run Code Online (Sandbox Code Playgroud)

因此,如果我将绑定表达式更改为"Path=DataContext.CurrentItem.CurveSpeedMustBeSpecified"它可以工作,但只要父用户控件的datacontext是a BindingListCollectionView.这是不可接受的,因为用户控件的其余部分会自动绑定到CurrentItemon的属性BindingList.

如何在样式中指定绑定表达式,以便无论父数据上下文是集合视图还是单个项目,它都可以工作?

data-binding wpf xaml relativesource datatemplate

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

如何在标签上设置目标属性?

以下代码:

    <TextBlock Name="foo"></TextBlock>
    <Label Target="foo">_Delta pressure</Label>
Run Code Online (Sandbox Code Playgroud)

生成以下设计时错误:

错误1无法将值'foo'分配给属性'Target'.属性'UIElement'的属性'Target'不能指定为字符串.C:\ Programming\WpfCustomPlot\SPT.Olga.Plot.Custom\PumpCurves\View\RatedValuesView.xaml 65 45 SPT.Olga.Plot.Custom

以下运行时错误:

'UIElement'类型没有公共TypeConverter类.第65行位置错误45.

我究竟做错了什么?

wpf

17
推荐指数
2
解决办法
7335
查看次数

为什么contentpresenter不填充网格单元?

好的,我在网格单元格中有一个contentpresenter:

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <WrapPanel Grid.Row="0" Grid.Column="0">
                <RadioButton GroupName="a" IsChecked="{Binding Path=SpecifyRatedValues, Mode=TwoWay}">Specify</RadioButton>
                <RadioButton GroupName="b" IsChecked="{Binding Path=SpecifyRatedValues, Converter={StaticResource invertBoolean}}">Auto generate</RadioButton>
            </WrapPanel>

            <Border BorderBrush="Black" BorderThickness="3" Grid.Row="1" Grid.Column="0">
                <ContentPresenter Content="{Binding Path=RatedValues}"></ContentPresenter>
            </Border>                    
        </Grid>
Run Code Online (Sandbox Code Playgroud)

contentpresenter查找资源下定义的datatemplate要使用的UI元素:

    <DataTemplate DataType="{x:Type ViewModels:RatedValuesViewModel}">
        <Views:RatedValuesView />
    </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

现在,一切都按预期工作,除了一件事:在运行时放置在contentpresenter内部的视图不会扩展以填充整个单元格.它在左侧和右侧留下了很大的余量.

如何使contentpresenter中的视图填充整个可用区域?

wpf

11
推荐指数
2
解决办法
9804
查看次数

AutoMockContainer,支持具有非接口依赖性的automocking类

我有一个具有非接口依赖的构造函数:

public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator)
Run Code Online (Sandbox Code Playgroud)

我正在使用Moq.Contrib automockcontainer.如果我尝试自动锁定MainWindowViewModel类,由于WeekNavigatorViewModel依赖项,我收到错误.

是否有任何automocking容器支持非接口类型的模拟?

正如马克在下面所示; 是的你可以!:-)我将Moq.Contrib AutoMockContainer替换为Mark在他的答案中提出的东西,唯一的区别是自动生成的模拟被注册为单例,但你可以使这个可配置.这是最终的解决方案:

/// <summary>
/// Auto-mocking factory that can create an instance of the 
/// class under test and automatically inject mocks for all its dependencies.
/// </summary>
/// <remarks>
/// Mocks interface and class dependencies
/// </remarks>
public class AutoMockContainer
{
    readonly IContainer _container;

    public AutoMockContainer(MockFactory factory)
    {
        var builder = new ContainerBuilder();

        builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
        builder.RegisterSource(new MoqRegistrationSource(factory));

        _container = builder.Build();
    }

    /// <summary>
    /// Gets or creates a mock …
Run Code Online (Sandbox Code Playgroud)

c# tdd moq mocking

9
推荐指数
1
解决办法
3454
查看次数

如何在PowerShell中编写Xml.linq?

我正试图在powershell中这样做:

XDocument document = XDocument.Load(@"web.config");

var comments = document.Descendants("client").DescendantNodes().OfType<XComment>().ToArray();

foreach (var comment in comments)
{
    XElement unCommented = XElement.Parse(comment.Value);
    comment.ReplaceWith(unCommented);
}
Run Code Online (Sandbox Code Playgroud)

我尝试过这样的事情:

$xDoc = [System.Xml.Linq.XDocument]::Load("web.config")

[System.Collections.Generic.IEnumerable[System.Xml.Linq.XElement]] $enum = $xDoc.Descendants("client")

$clients = [System.Xml.Linq.Extensions]::DescendantNodes($enum)
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误说"异常调用带有1个参数的DescendantNodes:值不能为null"

xml powershell linq-to-xml

9
推荐指数
2
解决办法
8783
查看次数

StructureMap:我如何单元测试注册表类?

我有一个这样的注册表类:

public class StructureMapRegistry : Registry
{
    public StructureMapRegistry()
    {
        For<IDateTimeProvider>().Singleton().Use<DateTimeProviderReturningDateTimeNow>();
    }
Run Code Online (Sandbox Code Playgroud)

我想根据我的意图测试配置,所以我开始编写测试:

public class WhenConfiguringIOCContainer : Scenario
{
    private TfsTimeMachine.Domain.StructureMapRegistry registry;
    private Container container;

    protected override void Given()
    {
        registry = new TfsTimeMachine.Domain.StructureMapRegistry();
        container = new Container();
    }

    protected override void When()
    {
        container.Configure(i => i.AddRegistry(registry));
    }

    [Then]
    public void DateTimeProviderIsRegisteredAsSingleton()
    {
        // I want to say "verify that the container contains the expected type and that the expected type
        // is registered as a singleton
    }
}
Run Code Online (Sandbox Code Playgroud)

如何验证注册表是否符合我的期望?注意:我介绍了容器,因为我没有在Registry类上看到任何可用的验证方法.理想情况下,我想直接在注册表类上进行测试.

.net c# structuremap

8
推荐指数
1
解决办法
2997
查看次数

当我们有Specflow时,为什么要使用编码的ui?

我们在当前项目中使用Specflow和WatIn进行验收测试.客户希望我们使用Microsoft编码的ui.我从来没有测试过编码的ui,但是到目前为止我看到它看起来很麻烦.我想在我有一个ui之前预先指定我的验收测试,而不是由于一些记录/回放的东西.无论如何,有人可以告诉我为什么我们应该扔掉Specflow/watin组合并用编码的ui替换它?

我还读过你可以将specflow和编码的ui结合起来,但对于我在specflow中已经做得很好的事情来说,它看起来很像开销.

coded-ui-tests specflow

8
推荐指数
1
解决办法
4443
查看次数

nhibernate:实现平等的最佳做法是什么?

我认为实体应该通过主键比较实现相等作为默认值,但是nhibernate文档建议使用业务标识:

最明显的方法是通过比较两个对象的标识符值来实现Equals()/ GetHashCode().如果值相同,则两者必须是相同的数据库行,因此它们是相等的(如果两者都添加到ISet,我们在ISet中只有一个元素).不幸的是,我们不能使用这种方法.NHibernate只会将标识符值分配给持久化的对象,新创建的实例将不具有任何标识符值!我们建议使用Business key equality实现Equals()和GetHashCode().

业务键平等意味着Equals()方法仅比较构成业务键的属性,这是一个在现实世界中标识我们实例的键(自然候选键)

和示例(也来自文档):

public override bool Equals(object other)
{
    if (this == other) return true;

    Cat cat = other as Cat;
    if (cat == null) return false; // null or not a cat

    if (Name != cat.Name) return false;
    if (!Birthday.Equals(cat.Birthday)) return false;

    return true;
}
Run Code Online (Sandbox Code Playgroud)

这让我头晕目眩,因为业务标识的概念(根据示例)与通过语法进行比较相同,这基本上是我与ValueObjects相关联的语义类型.不使用数据库主键作为比较值的原因是,如果未在客户端生成主键(对于ex incremental),则会更改对象的哈希码,并且您使用某种哈希表集合(例如ISet)用于存储您的实体.

我怎样才能创建一个良好的相等实现,它不会破坏相等/ hashcode的一般规则(http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx)并且也符合nhibernate规则?

nhibernate equality

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

nhibernate连接释放模式:为什么文档推荐使用"after_transaction"?

hibernate文档说明如下:

配置参数hibernate.connection.release_mode用于指定要使用的发布模式.可能的值:*auto(默认值) - 相当于当前版本中的after_transaction.由于此设置的值导致的失败往往表示用户代码中的错误和/或无效假设,因此更改此默认行为并不是一个好主意.*on_close - 表示使用ConnectionReleaseMode.OnClose.此设置留待向后兼容,但强烈建议不要使用它....

我创建了一个集成测试,它通过同时打开两个会话并操纵同一个实体来激发StaleObjectException.为确保测试在完成后回滚所有内容,测试内容将放在TransactionScope中; 这会导致分布式事务启动,因为两个会话将针对同一环境事务打开数据库连接.我想将默认的ConnectionReleaseMode设置更改为"OnClose",但如上所述,文档不建议这样做.任何人都可以解释为什么改变默认行为不是一个好主意?

nhibernate

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