在WPF中,您可以将其设置为TargetType
类型的名称,也可以将其设置为{x:Type nameOfType}
.
有谁知道有什么区别?
我正在考虑为我的应用程序编写WPF用户控件.我在我的应用程序中使用MVVM.
用户控件可能需要可以设置为父视图的依赖项属性.当使用MVVM时,想法是父视图最终将在UserControls
DP与父视图的VM 之间创建绑定)
Dependency Properties
需要在View类中创建,因为VM不会继承DependencyObject
.这意味着在XAML代码中添加代码.
我想知道你是否可以就使用MVVM开发WPF应用程序时如何设计用户控件提出建议......
我所希望做的是一个类似的原则,以添加附件的电子邮件,你可以点击一个按钮和一个新的浏览框将打开上升单独的附件,你可以拥有的数量.
我是新人,所以如果有人能指出我的榜样?
我已经设法将其简化为一个简单的测试用例.在使用以下方法解析此XAML期间抛出异常XamlReader.Parse()
:
<DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Background" Value="#FFEEEEEE" />
</Trigger>
</Style.Triggers>
</Style>
</DockPanel.Resources>
<TextBox IsReadOnly="True" />
</DockPanel>
Run Code Online (Sandbox Code Playgroud)
异常消息是:
无法设置未知成员'System.Windows.Controls.TextBox.IsReadOnly'.行号'13'和行位置'11'.
如果我不设置IsReadOnly
上TextBox
,它解析罚款.如果我删除样式触发器,它也会解析.
任何人都可以对此有所了解吗?我对WPF很新.
更新:
这是我用来重现这个的单元测试(它在我的电脑上失败了):
[TestMethod]
public void TestIsReadOnlyOnTextBox()
{
// Arrange
var xaml =
@"<DockPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<DockPanel.Resources>
<Style TargetType=""TextBox"">
<Style.Triggers>
<Trigger Property=""IsReadOnly"" Value=""True"">
<Setter Property=""Background"" Value=""#FFEEEEEE"" />
</Trigger>
</Style.Triggers>
</Style>
</DockPanel.Resources>
<TextBox IsReadOnly=""True"" />
</DockPanel>
";
// Act
try {
var root = XamlReader.Parse(xaml);
}
catch (XamlParseException ex) …
Run Code Online (Sandbox Code Playgroud) 我试图CallerMemberName
通过BCL可移植包在.NET 4.0中使用属性.它总是返回一个空字符串而不是成员名称.我究竟做错了什么?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MessageBox.Show(new class2().CallMe);
}
}
public class class2
{
public string CallMe
{
get
{
return HelpMe();
}
}
private string HelpMe([CallerMemberName] string param = "")
{
return param;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个VS2008,C#WPF,Excel AddIn; 在某些情况下,我的插件会抛出异常
A first chance exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
A first chance exception of type 'System.NullReferenceException' occurred in PresentationFramework.dll
Run Code Online (Sandbox Code Playgroud)
但我找不到异常的来源.我知道这是b/c数据绑定.但无法找出原因.每次我介入时,VS都会跟踪一个执行w/o错误的方法,然后抛出异常,但不知道哪行代码.
我这几天一直在苦苦挣扎,无法取得一些进展.请帮忙.谢谢
编辑,它太长,不适合评论.所以我把xaml文件放在这里.抛出异常的@xmal文件.这是DataGridComboBoxColumn
抛出异常
<UserControl x:Class="View.BasketView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit" >
<UserControl.Resources>
<sharedC:FunctionToHiddenVisibility x:Key="enumSRToVis"/>
<sharedC:FunctionToHiddenVisibility x:Key="enumCSToVis"/>
<Style x:Key="DataGridRowStyle" TargetType="{x:Type dg:DataGridRow}">
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1" >
<Setter Property="Background" Value="Beige" />
</Trigger>
</Style.Triggers>
<Setter Property="AllowDrop" Value="True" />
<Setter Property="Margin" Value="0 2 0 2" />
</Style>
<Style x:Key="DataGridStyle" TargetType="{x:Type dg:DataGrid}">
<Setter Property="AlternationCount" Value="2" />
<Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}" />
</Style> …
Run Code Online (Sandbox Code Playgroud) 我想创建一个只接受特定范围内的数值的TextBox.实现此类TextBox的最佳方法是什么?
我想到了派生TextBox并覆盖TextProperty的验证和强制.但是,我不知道如何做到这一点,我知道通常不建议派生WPF控件.
private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
int result;
if (!validateStringAsNumber(e.Text,out result,false))
{
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
(validateStringAsNumber是我主要使用Int.TryParse的函数)
一些建议的解决方案可能更好,但对于我需要的简单功能,这个解决方案是最容易和最快的,同时足以满足我的需求.
有没有一种简单的方法在StackPanel内的项目之间设置默认空间,所以我不必在每个项目上设置Margin属性?
我想在TFS中使用Tortoise SVN diff实用程序.我知道如何在TFS中配置用户工具.我需要的是Tortoise SVN diff实用程序的安装程序.我想我需要安装完整的Tortoise SVN才能获得SVN diff工具,是否可以下载这个工具的独立版本?
我知道其他各种差异工具都可以使用,比如winMerge等,但我对龟龟svn diff非常熟悉.
我在SO和其他网站上已经阅读了很多依赖属性.但是,真的没有找到一个很好的解释,我仍然感到困惑.我同时使用SL和WPF.在实施方面,它们在SL和WPF方面有所不同吗?为什么我们真的需要它们?它们是否静态意味着它们的价值是共享的?为什么MS引入依赖属性的原因?
赏金: 我正在寻找一个更彻底,更完整的答案.
wpf ×7
c# ×5
.net ×4
xaml ×3
.net-4.0 ×1
add-in ×1
debugging ×1
diff ×1
excel ×1
java ×1
mvvm ×1
silverlight ×1
stackpanel ×1
swing ×1
targettype ×1
textbox ×1
tfs ×1
tortoisesvn ×1
xamlreader ×1