小编haa*_*gel的帖子

将Html.BeginForm()与自定义路由一起使用

这是你肯定知道的默认路线:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Start", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)

假设我使用BeginForm()方法,如下所示:

@using (Html.BeginForm("MyAction", "MyController", new { id = 4 }))
Run Code Online (Sandbox Code Playgroud)

这将呈现以下表单标记:

<form method="post" action="/MyController/MyAction/4">
Run Code Online (Sandbox Code Playgroud)

现在,假设我已经制定了自定义路线:

routes.MapRoute(
    "MyCustomRoute", // Route name
    "MyController/{id}/{action}", // URL with parameters
    new { controller = "MyController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)

当我创建一个表单时,我希望它看起来像这样:

<form method="post" action="/MyController/4/MyAction">
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用上面示例中的BeginForm(),我将得到一个匹配默认路由的url.有没有办法告诉BeginForm()在创建动作的URL时使用我的自定义路由而不是默认路由?或者BeginForm()总是生成遵循默认路由模式的URL?

如果重要的话,我正在使用asp.net mvc 3.

asp.net-mvc asp.net-mvc-3

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

使用Rhino Mocks从模拟/存根中提升事件

如何使用Rhino Mocks从模拟/存根中提取事件?

我在网上找到了这个问题的一些答案,但它们似乎都使用了Record/Replay语法,但我使用的是Arrange/Act/Assert语法.

有什么建议?

一个小例子......

假设我正在使用MVVM模式并拥有此模型类:

public class MyModel
{
    private int _myValue;

    public event EventHandler ValueChanged;

    public void SetValue(int newValue)
    {
        _myValue = newValue;
        if (ValueChanged != null)
        {
            ValueChanged(this, null);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,该类具有整数值和设置它的方法.设置该值时,将ValueChanged引发一个事件.

此模型类由viewmodel使用:

public class MyViewModel
{
    private MyModel _myModel;

    public MyViewModel(MyModel myModel)
    {
        _myModel = myModel;
        _myModel.ValueChanged += ValueChangedHandler;
    }

    private void ValueChangedHandler(object sender, EventArgs e)
    {
        MyString = "Value has changed";
    }

    public string MyString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

此视图ValueChanged …

c# events unit-testing rhino-mocks

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

WPF:使用TemplateBinding将整数绑定到TextBlock

我在WPF中有一个自定义控件.在这我有一个DependencyProperty类型int.在自定义控件的模板中我有一个TextBlock,我想在中显示整数的值TextBlock.但我无法让它发挥作用.

我正在使用TemplateBinding.如果我使用相同的代码,但是改变的类型DependencyProperty,以string正常工作.但我真的希望它是我的应用程序其余部分工作的整数.

我怎样才能做到这一点?

我编写了简化的代码来显示问题.首先是自定义控件:

public class MyCustomControl : Control
{
    static MyCustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));

        MyIntegerProperty = DependencyProperty.Register("MyInteger", typeof(int), typeof(MyCustomControl), new FrameworkPropertyMetadata(0));
    }


    public int MyInteger
    {
        get
        {
            return (int)GetValue(MyCustomControl.MyIntegerProperty);
        }
        set
        {
            SetValue(MyCustomControl.MyIntegerProperty, value);
        }
    }
    public static readonly DependencyProperty MyIntegerProperty;
}
Run Code Online (Sandbox Code Playgroud)

这是我的默认模板:

<Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                <Border BorderThickness="1" CornerRadius="4" BorderBrush="Black" Background="Azure">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{TemplateBinding MyInteger}" HorizontalAlignment="Center" …
Run Code Online (Sandbox Code Playgroud)

wpf xaml binding controltemplate

15
推荐指数
1
解决办法
6759
查看次数

在ASP.NET MVC 4中使用和不使用控制器名称进行路由

我正在使用ASP.NET MVC 4,我在设置路由时遇到了一些问题.您能否告诉我如何设置我的路由以将URL指向操作,如下所示:

  • "/"(或"/ Start")=> PublicController.Start()
  • "/ About"=> PublicController.About()
  • "/ MyPage"(或"/ MyPage/Summary")=> MyPageController.Summary()
  • "/ MyPage/Invoices"=> MyPageController.Invoices()
  • "/ MyPage/Invoice/72"=> MyPageController.Invoice(int id)

这是网址"/关于"让我感到烦恼,即一个没有指定控制器的网址.如果我使那个工作指定控制器的其他人停止工作.我可以为"/ About"创建一个单独的控制器,我想,但是如果我不需要,我宁愿不这样做(我有更多的网址遵循该模式).

asp.net-mvc asp.net-mvc-routing asp.net-mvc-4

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

如何找到最大化窗口的位置?

我需要知道最大化窗口的位置.

WPF窗口具有Top和Left属性,用于指定窗口的位置.但是,如果最大化窗口,这些属性会使窗口的值保持正常状态.

如果您在单屏设置上运行,则最大化位置自然是(0,0).但是,如果您有多个屏幕不一定是真的.如果在主屏幕上最大化窗口,窗口将只有位置(0,0).

那么......有没有办法找出最大化窗口的位置(最好是与Top和Left属性相同的逻辑单元)?

wpf window

13
推荐指数
5
解决办法
7596
查看次数

WPF:TextBox扩展与周围的网格,但不与文本

我在应用程序中遇到TextBox问题...

窗口有一个带两列的网格.左列包含一个具有恒定宽度但具有适应高度的控件.右列包含一个TextBox,它占用Grid中的所有剩余空间(从而占用Window).

Grid具有最小的宽度和高度,并包含在ScrollViewer中.如果用户将窗口的大小调整为小于Grid的最小widht/height,则会显示滚动条.

这正是我想要的.但是,当用户开始键入文本时会出现问题.如果文本要长到适合TextBox中的一行,我希望文本换行.因此我在TextBox上设置了TextWrapping ="Wrap".但由于TextBox具有自动宽度并且包含在ScrollViewer中(实际上是包裹的整个Grid),因此TextBox只是向右扩展.

如果窗口展开,我确实希望TextBox扩展,但我不希望TextBox按文本扩展.而是文本应该包装在可用的TextBox中.如果文本不适合TextBox高度,则应在TextBox中显示滚动条.

有没有办法实现这个目标?

下面是一些显示我的问题的代码.

<Window x:Class="AdaptingTextBoxes.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="300" Width="400" Background="DarkCyan">
<Grid Margin="10" Name="LayoutRoot">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Grid MinWidth="300" MinHeight="200">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="0" Margin="0,0,10,0" Content="Button" Width="100" />

            <TextBox Grid.Column="1" AcceptsReturn="True" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" />
        </Grid>
    </ScrollViewer>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

wpf xaml textbox autosize

11
推荐指数
1
解决办法
5765
查看次数

WPF:空时隐藏ContextMenu

我有一个上下文菜单,通过数据绑定获取菜单项(我使用的是MVVM模式):

<ContextMenu ItemsSource="{Binding Path=ContextMenuItems}" />
Run Code Online (Sandbox Code Playgroud)

这很好用.但是,在没有要显示的菜单项的情况下,我不希望上下文菜单显示.有没有办法实现这个目标?某种XAML触发可能吗?

当没有孩子时,我已经尝试捕获Opened事件,关闭上下文菜单.这工作但上下文菜单仍然闪烁...

wpf contextmenu mvvm

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

MouseOver和MouseDown上的动画按钮

我正在为WPF中的标准Button创建自己的ControlTemplate.当用户用鼠标悬停在按钮上时,我想更改按钮的背景,当用户按下按钮时(另一种颜色),我想更改按钮的背景.这似乎是一种常见的行为,但我无法让它发挥作用.

我的模板由一个带有图像的边框组成.它是我想要制作动画的边框的背景颜色(真正的渐变).我的模板中有触发器可以激活动画(故事板).

MouseOver/Out工作得很好.按下按钮时出现问题.Press动画应该运行,Release动画也是如此.但在此之后,MouseOut永远不会运行.该按钮卡在MouseOver状态.

我究竟做错了什么?

<ControlTemplate TargetType="{x:Type Button}">
    <ControlTemplate.Resources>
        <Storyboard x:Key="MouseOverAnimation">
            <ColorAnimation Storyboard.TargetName="ButtonBorderGradientStop1" Storyboard.TargetProperty="Color" To="#ffefefff" Duration="0:0:0.2" />
            <ColorAnimation Storyboard.TargetName="ButtonBorderGradientStop2" Storyboard.TargetProperty="Color" To="#ffc7c7ff" Duration="0:0:0.2" />
        </Storyboard>
        <Storyboard x:Key="MouseOutAnimation">
            <ColorAnimation Storyboard.TargetName="ButtonBorderGradientStop1" Storyboard.TargetProperty="Color" To="#ffeeeeee" Duration="0:0:0.2" />
            <ColorAnimation Storyboard.TargetName="ButtonBorderGradientStop2" Storyboard.TargetProperty="Color" To="#ffcccccc" Duration="0:0:0.2" />
        </Storyboard>
        <Storyboard x:Key="MouseDownAnimation">
            <ColorAnimation Storyboard.TargetName="ButtonBorderGradientStop1" Storyboard.TargetProperty="Color" To="#ffc7c7ff" Duration="0:0:0.1" />
            <ColorAnimation Storyboard.TargetName="ButtonBorderGradientStop2" Storyboard.TargetProperty="Color" To="#ff9a9aff" Duration="0:0:0.1" />
        </Storyboard>
        <Storyboard x:Key="MouseUpAnimation">
            <ColorAnimation Storyboard.TargetName="ButtonBorderGradientStop1" Storyboard.TargetProperty="Color" To="#ffefefff" Duration="0:0:0.1" />
            <ColorAnimation Storyboard.TargetName="ButtonBorderGradientStop2" Storyboard.TargetProperty="Color" To="#ffc7c7ff" Duration="0:0:0.1" />
        </Storyboard>
    </ControlTemplate.Resources>


    <Border x:Name="ButtonBorder" CornerRadius="0" BorderBrush="#55aaaaaa" BorderThickness="1" Width="23" Height="22">
        <Border.Background>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> …
Run Code Online (Sandbox Code Playgroud)

wpf animation xaml

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

在代码中更改ELMAH数据库

我有几个网站有单独的ELMAH数据库.我想有一个单独的网站来查看所有这些数据库.

我为此创建了一个新的ASP.NET MVC 4网站.我添加了NuGet包"Elmah.MVC"并将以下内容添加到Web.config:

<connectionStrings>
    <add name="Elmah.Sql" connectionString="..." />
</connectionStrings>
<elmah>
    <errorLog type="Elmah.SqlErrorLog" connectionStringName="Elmah.Sql" />
</elmah>
Run Code Online (Sandbox Code Playgroud)

这适用于单个数据库.我也可以通过更改连接字符串来切换数据库,但现在我希望能够从代码中切换数据库.有没有办法做到这一点?

.net asp.net-mvc connection-string elmah

8
推荐指数
2
解决办法
2326
查看次数

WPF MVVM - 将UserControls上的属性绑定到容器的ViewModel

我有一个Window(MainWindow.xaml),它有一个ViewModel(MainWindowViewModel.cs).我还有一个名为MyUserControl.xaml的UserControl,它也有一个相应的ViewModel(MyUserControlViewModel.cs).

我已将UserControl的两个实例插入MainWindow:

<Window x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyProject"
    Title="My Window">
    <Grid>
        <local:MyUserControl Visibility="{Binding Path=MyUserControl1Visibility, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
        <local:MyUserControl Visibility="{Binding Path=MyUserControl2Visibility, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

在MainWindow的CodeBehind中,我将Window的DataContext设置为ViewModel的一个实例:

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new MainWindowViewModel();
}
Run Code Online (Sandbox Code Playgroud)

MainWindowViewModel具有MyUserControl实例绑定的Visibility属性.它们看起来都是这样的:

private Visibility _myUserControl1Visibility = Visibility.Collapsed;
public Visibility MyUserControl1Visibility
{
    get
    {
        return _myUserControl1Visibility;
    }
    private set
    {
        if (value != _myUserControl1Visibility)
        {
            _myUserControl1Visibility = value;
            OnPropertyChanged("MyUserControl1Visibility");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,MainWindow和MainWindowViewModel具有按钮和命令,使用户能够在两个MyUserControl实例之间切换.也就是说,它们中只有一个随时出现.

这工作正常......直到UserControls获得自己的ViewModels.现在,运行时尝试在UserControls的ViewModel上找到绑定的VisibilityProperties(MyUserControl1Visibility ...),而不是MainWindow的ViewModel.

如何将这些绑定转到MainWindowViewModel而不是UserControl实例的相应ViewModel?

wpf binding mvvm

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