小编H.B*_*.B.的帖子

如何在ListBox中获得垂直滚动条?

在下面的例子中,我有一个ListBox,里面有几十个字体名称.

我原以为它会自动在它上面有一个垂直滚动条,这样你就可以选择任何字体,而不仅仅是列表中的第一个字体,但事实并非如此.

所以我添加了一个"ScrollViewer"并在右侧放置了一个"滚动条区域",但滚动条区域中没有滚动条,因此您可以滚动(!).

为什么滚动条不是自动的,如何强制滚动条?

<StackPanel Name="stack1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <ScrollViewer>
            <ListBox Grid.Row="0" Name="lstFonts" Margin="3"  ItemsSource="{x:Static Fonts.SystemFontFamilies}"/>
        </ScrollViewer>
    </Grid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

wpf xaml listbox scrollviewer

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

在代码中设置边距属性

MyControl.Margin.Left = 10;
Run Code Online (Sandbox Code Playgroud)

错误:

无法修改'System.Windows.FrameworkElement.Margin'的返回值,因为它不是变量

c# wpf margin

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

捕获TextBox中的Enter键

在我的WPF视图中,我试图将事件绑定到Enter键,如下所示:

<TextBox Width="240" VerticalAlignment="Center" Margin="2" Text="{Binding SearchCriteria, Mode=OneWayToSource}">
  <TextBox.InputBindings>
      <KeyBinding Key="Enter" Command="{Binding EnterKeyCommand}"/>
      <KeyBinding Key="Tab" Command="{Binding TabKeyCommand}"/>
  </TextBox.InputBindings>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

此代码有效,当用户按下Enter键时,我的EnterKeyCommand将触发.但问题是,当事件触发时,WPF尚未将文本框中的文本绑定到"SearchCriteria".因此,当我的事件触发时,"SearchCriteria"的内容为空.我可以在此代码中进行简单的更改,以便在我的EnterKey命令触发时可以获取文本框的内容吗?

wpf

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

绑定到"设置"中定义的值

在WPF中,我可以使用与"设置"中定义的值绑定吗?如果可以,请提供样品.

wpf binding appsettings

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

在代码中设置WPF标签的Style属性?

在App.xaml中,我有以下代码:

<Application.Resources>
    <Style x:Key="LabelTemplate" TargetType="{x:Type Label}">
        <Setter Property="Height" Value="53" />
        <Setter Property="Width" Value="130" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Margin" Value="99,71,0,0" />
        <Setter Property="VerticalAlignment" Value= "Top" />
        <Setter Property="Foreground" Value="#FFE75959" />
        <Setter Property="FontFamily" Value="Calibri" />
        <Setter Property="FontSize" Value="40" />
    </Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

这是为了为我的标签提供通用模板.

在主要的XAML代码中,我有以下代码行:

<Label Content="Movies" Style="{StaticResource LabelTemplate}" Name="label1" />
Run Code Online (Sandbox Code Playgroud)

但是,我想通过代码初始化Style属性.我试过了:

label1.Style = new Style("{StaticResource LabelTemplate}");
Run Code Online (Sandbox Code Playgroud)

label1.Style = "{StaticResource LabelTemplate}";
Run Code Online (Sandbox Code Playgroud)

两种解决方案都无效.

任何帮助,将不胜感激 :).

c# wpf user-interface label

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

将月份int转换为月份名称

我只是试图使用该DateTime结构将1到12之间的整数转换为一个明确的月份名称.

这是我尝试过的:

DateTime getMonth = DateTime.ParseExact(Month.ToString(), 
                       "M", CultureInfo.CurrentCulture);
return getMonth.ToString("MMM");
Run Code Online (Sandbox Code Playgroud)

但是我得到了FormatException第一行,因为字符串不是有效的DateTime.谁能告诉我怎么做?

.net c# datetime .net-4.0

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

DataGrid在不活动时选择的行颜色

当DataGrid失去焦点时,如何设置WPF DataGrid的样式以更改所选行的颜色?

wpf wpfdatagrid

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

图像UriSource和数据绑定

我正在尝试将自定义对象列表绑定到WPF图像,如下所示:

<Image>
    <Image.Source>
        <BitmapImage UriSource="{Binding Path=ImagePath}" />
    </Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.这是我得到的错误:

必须设置"Property'UriSource'或属性'StreamSource'."

我错过了什么?

.net data-binding wpf xaml

64
推荐指数
6
解决办法
13万
查看次数

单个方法的管理员权限

是否可以为单个方法要求管理员权限?

像这样的东西:

[RequireAdminRightsForThisMethod()]

private void TheMethod(){

    // Do something

}
Run Code Online (Sandbox Code Playgroud)

c# permissions administrator

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

从Code Behind调用命令

所以我一直在四处寻找并且无法确切地知道如何做到这一点.我正在使用MVVM创建用户控件,并希望在'Loaded'事件上运行命令.我意识到这需要一些代码,但我无法弄清楚需要什么.该命令位于ViewModel中,它被设置为视图的datacontext,但我不确定如何路由它,所以我可以从加载的事件后面的代码中调用它.基本上我想要的是这样的......

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    //Call command from viewmodel
}
Run Code Online (Sandbox Code Playgroud)

环顾四周,我无法在任何地方找到这种语法.我是否需要先在xaml中绑定命令才能引用它?我注意到用户控件中的命令绑定选项不会让你像按钮那样绑定命令......

<UserControl.CommandBindings>
    <CommandBinding Command="{Binding MyCommand}" /> <!-- Throws compile error -->
</UserControl.CommandBindings>
Run Code Online (Sandbox Code Playgroud)

我确信有一个简单的方法可以做到这一点,但我不能为我的生活弄清楚.

c# data-binding wpf xaml command

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