在下面的例子中,我有一个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) MyControl.Margin.Left = 10;
Run Code Online (Sandbox Code Playgroud)
错误:
无法修改'System.Windows.FrameworkElement.Margin'的返回值,因为它不是变量
在我的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命令触发时可以获取文本框的内容吗?
在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)
两种解决方案都无效.
任何帮助,将不胜感激 :).
我只是试图使用该DateTime结构将1到12之间的整数转换为一个明确的月份名称.
这是我尝试过的:
DateTime getMonth = DateTime.ParseExact(Month.ToString(),
"M", CultureInfo.CurrentCulture);
return getMonth.ToString("MMM");
Run Code Online (Sandbox Code Playgroud)
但是我得到了FormatException第一行,因为字符串不是有效的DateTime.谁能告诉我怎么做?
当DataGrid失去焦点时,如何设置WPF DataGrid的样式以更改所选行的颜色?
我正在尝试将自定义对象列表绑定到WPF图像,如下所示:
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ImagePath}" />
</Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.这是我得到的错误:
必须设置"Property'UriSource'或属性'StreamSource'."
我错过了什么?
是否可以为单个方法要求管理员权限?
像这样的东西:
[RequireAdminRightsForThisMethod()]
private void TheMethod(){
// Do something
}
Run Code Online (Sandbox Code Playgroud) 所以我一直在四处寻找并且无法确切地知道如何做到这一点.我正在使用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)
我确信有一个简单的方法可以做到这一点,但我不能为我的生活弄清楚.
wpf ×8
c# ×5
xaml ×3
.net ×2
data-binding ×2
.net-4.0 ×1
appsettings ×1
binding ×1
command ×1
datetime ×1
label ×1
listbox ×1
margin ×1
permissions ×1
scrollviewer ×1
wpfdatagrid ×1