为什么我们真的需要用户控件?
窗口:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication1="clr-namespace:WpfApplication1">
<wpfApplication1:SaveCloseUserControl />
</Window>
Run Code Online (Sandbox Code Playgroud)
用户控制:
<UserControl x:Class="WpfApplication1.SaveCloseUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal">
<Button Height="30" Content="Save" />
<Button Height="30"
Margin="1"
Content="Cancel" />
</StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
代码背后:
public partial class SaveCloseUserControl : UserControl
{
public SaveCloseUserControl()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
我没有看到任何理由为什么我应该在UserControl中包装StackPanel(或任何其他控件),如果没有UserControl的以下代码将完全相同.
窗口:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication1="clr-namespace:WpfApplication1">
<wpfApplication1:SaveCloseStackPanel />
</Window>
Run Code Online (Sandbox Code Playgroud)
没有用户控制的堆栈面板:
<StackPanel x:Class="WpfApplication1.SaveCloseUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Orientation="Horizontal">
<Button Height="30" Content="Save" />
<Button Height="30"
Margin="1"
Content="Cancel" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
代码背后:
public partial class SaveCloseUserControl : StackPanel
{
public SaveCloseUserControl()
{
InitializeComponent(); …Run Code Online (Sandbox Code Playgroud) 我遇到了一些可能是wpf列表框中的错误的东西.请查看代码然后我解释会发生什么
窗口
<Window >
<Grid>
<local:MultiSelectionComboBox Width="200"
MinHeight="25"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ASLDisplayMemberPath="FirstName"
ASLSelectedItems="{Binding SelectedModels}"
ItemsSource="{Binding Models}" />
<ListBox HorizontalAlignment="Right"
VerticalAlignment="Top"
DisplayMemberPath="FirstName"
ItemsSource="{Binding SelectedModels}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
用户控制
<ComboBox>
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="MainGrid"
Width="Auto"
Height="Auto"
SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="0" />
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup"
Grid.ColumnSpan="2"
Margin="1"
AllowsTransparency="true"
IsOpen="{Binding Path=IsDropDownOpen,
RelativeSource={RelativeSource TemplatedParent}}"
Placement="Center"
PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
<Border x:Name="DropDownBorder"
MinWidth="{Binding Path=ActualWidth,
ElementName=MainGrid}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<ScrollViewer CanContentScroll="true">
<StackPanel>
<CheckBox x:Name="checkBox"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=CheckAllCommand}">select …Run Code Online (Sandbox Code Playgroud) 我只是想知道C#6中的nameof如何才能访问非静态属性,就像它是静态的一样.这是一个例子
public class TestClass
{
public string Name { get; set; }
}
public class Test
{
public Test()
{
string name = nameof(TestClass.Name); // whats so speciall about nameof
//string name2 = TestClass.Name; this won't compile obviously,
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试做一些人认为应该非常简单的事情(至少在WPF中).我有一个包含listbox和datatemplate的页面,现在datatemplate调用了一个带有按钮的用户控件.没什么好看的,但是按钮命令不是listboxsource的一部分,我找不到一个简单的方法来告诉按钮在哪里查找命令.这是场景
<Page x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1">
<Page.Resources>
<DataTemplate x:Key="MyDataTemplate">
<local:MyButton />
</DataTemplate>
</Page.Resources>
<ListBox ItemTemplate="{StaticResource MyDataTemplate}" ItemsSource="{Binding Customers}" />
</Page>
<UserControl x:Class="App1.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=2}, Path=DataContext.DeleteCommand}" Content="Delete" />
</UserControl>
Run Code Online (Sandbox Code Playgroud)
请注意这不编译,因为在UWP中没有模式找到祖先?我应该怎么做,我一直在看谷歌但是找不到任何关于它的东西.
谢谢
在c#中我可以用速记做这样的事情吗?
bool validName = true;
if (validName)
{
name = "Daniel";
surname = "Smith";
}
else
{
MessageBox.Show("Invalid name");
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道类似的东西是否会起作用,但在这个确切的场景中,我知道你可以分配值,如果我的名字= validName?"丹尼尔":"无效",但我只是想知道我能否做到以下?
validName ?
{
name = "Daniel";
surname = "Smith";
}
:
{
MessageBox.Show("Invalid name");
}
Run Code Online (Sandbox Code Playgroud) 当我向上或向下按箭头键一次时,为什么列表框从最后一条记录跳到第一条?
以下是如何重现此问题
主窗口
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListBox x:Name="MyListbox"
ItemsSource="{Binding Entities}"
SelectedItem="{Binding SelectedEntity}" />
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
MyListbox.Focus();
}
}
Run Code Online (Sandbox Code Playgroud)
视图模型
public class MainWindowViewModel : NotifyPropertyChanged
{
public MainWindowViewModel()
{
Entities = new ObservableCollection<string>()
{
"Batman",
"Superman",
"Shrek",
"Jack Frost",
"Wolverine"
};
SelectedEntity = Entities.Last();
}
public ObservableCollection<string> Entities { get; set; }
private string selectedEntity;
public string SelectedEntity
{
get { return selectedEntity; }
set { OnPropertyChanged(ref …Run Code Online (Sandbox Code Playgroud) 这是微不足道的事情,但它不起作用.
我有这样的东西(它在自己的文件夹中)
<ResourceDictionary>
<Path x:Key="Test"
Stroke="Black"
Fill="Gray"
Data="M 10,100 C 10,300 300,-200 300,100" />
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
现在我想用它
<Page>
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergeDictionaries>
<ResourceDictionary Source="MyFolder/MyResourceDictionary.xaml/>
</ResourceDictionary.MergeDictionaries>
</ResourceDictionary>
</Page.Resources>
<ContentPresenter Content="{StaticResource Test}"/>
<Page/>
Run Code Online (Sandbox Code Playgroud)
这会引发异常,但我不明白为什么.在wpf中完全相同的场景工作正常.
在WPF中这样的东西会告诉我边界左上角的位置:
var point = myBorder.PointToScreen(new Point());
Run Code Online (Sandbox Code Playgroud)
如何在UWP中获得相同的内容?我找不到任何方法来获得它.
谢谢 :)