我完全迷失在MVVM中使用的命令绑定中.我应该如何将我的对象绑定到窗口和/或它的命令控件来获取方法Button Click?
这是一CustomerViewModel堂课:
public class CustomerViewModel : ViewModelBase
{
RelayCommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand(param => this.Save(), param => this.CanSave);
NotifyPropertyChanged("SaveCommand");
}
return _saveCommand;
}
}
public void Save()
{
...
}
public bool CanSave { get { return true; } }
...
Run Code Online (Sandbox Code Playgroud)
ViewModelBase实现INotifyPropertyChanged接口以下是Button命令的绑定方式:
<Button Content="Save" Margin="3" Command="{Binding DataContext.Save}" />
Run Code Online (Sandbox Code Playgroud)
将一个实例CustomerViewModel分配给DataContext包含a的窗口Button.
给定的示例不起作用:我已将断点放入Save方法中,但执行不会传递给方法.我已经看到很多例子(在stackoverflow上也是如此),但是无法弄清楚应该如何指定绑定. …
在调试我的WPF程序时,我注意到当窗口是设置大小时,控制看起来很好.但是当窗口最大化时,内容位于相同的位置,就好像窗口没有调整大小一样.我想要它,以便内容和窗口按比例调整大小.我怎样才能做到这一点?对不起,如果这是一个noobish问题,但我在WPF时代有点新.
XAML代码还没有完全准备好,但这里有一些元素:
<DockPanel>
<StackPanel DockPanel.Dock="Left">
...
</StackPanel>
<TabControl DockPanel.Dock="Right">
...
</TabControl>
<ListView>
...
</ListView>
</DockPanel>
Run Code Online (Sandbox Code Playgroud) 所以这是我的TextBlock:
<TextBlock Text="1 Projects / 1 Issues"></TextBlock>
Run Code Online (Sandbox Code Playgroud)
使用数据绑定我想更换1和2 {Binding Path=OpenProjects}和{Binding Path=OpenIssues}.做这个的最好方式是什么?
PS我没有结婚TextBlock.
在 WPF 中,当用户在 中获得焦点时TextBox,我想要一些动画,该动画可以使TextBox变成多行并使其Width变大(当他打字时),并且当焦点丢失时,它TextBox会恢复到其原始大小。
大小未知。
此外,最终,这TextBox需要在 WPF 中DataGrid。
我以前从未做过动画,希望能帮助我入门。谢谢。
编辑:我已经成功地制作了动画,同时具有一些固定的宽度值(使其多行不起作用,但这并不重要)。所以我现在的问题是,如果这是未知的,我怎样才能回到原来的尺寸。我可以在Width房产上使用乘数吗?
到目前为止,这是我的代码:
<Window.Resources>
<Storyboard x:Key="GrowStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(FrameworkElement.Width)">
<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="400" KeySpline="0.54,0.27,0.38,0.69"/>
</DoubleAnimationUsingKeyFrames>
<Int32Animation Duration="0:0:0.4" From="1" To="3" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(TextBox.MinLines)">
</Int32Animation>
</Storyboard>
<Storyboard x:Key="ShrinkStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(FrameworkElement.Width)">
<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="200" KeySpline="0.54,0.27,0.38,0.69"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="FocusManager.GotFocus" SourceName="textBox">
<BeginStoryboard x:Name="GrowStoryboard_BeginStoryboard" Storyboard="{StaticResource GrowStoryboard}"/>
</EventTrigger>
<EventTrigger RoutedEvent="FocusManager.LostFocus" SourceName="textBox">
<BeginStoryboard x:Name="ShrinkStoryboard_BeginStoryboard" Storyboard="{StaticResource ShrinkStoryboard}"/>
</EventTrigger>
</Window.Triggers>
<StackPanel>
<TextBox x:Name="textBox" …Run Code Online (Sandbox Code Playgroud) 我使用基于属性的验证为WPF构建了自己的自定义验证框架.我坚持最后一步是突出显示TextBox.实际上,它确实突出显示了文本框,但所有文本框都依赖于单个属性HasError.
public class RegistrationViewModel : ViewModel
{
[NotNullOrEmpty("FirstName should not be null or empty")]
public string FirstName { get; set; }
[NotNullOrEmpty("Middle Name is required!")]
public string MiddleName { get; set; }
[NotNullOrEmpty("LastName should not be null or empty")]
public string LastName { get; set; }
public bool HasError
{
get
{
**return Errors.Count > 0; // THIS IS THE PROBLEM**
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是XAML代码:
<Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=HasError}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers> …Run Code Online (Sandbox Code Playgroud) 我有两种类型的文本需要遵循基于枚举的类似着色规则:
public enum Modes
{
A,
B,
C
}
Run Code Online (Sandbox Code Playgroud)
带DataTrigger标记的样式用于着色:
<Style TargetType="SEE BELOW" x:Key="Coloring">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=.}" Value="A">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=.}" Value="B">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=.}" Value="C">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
一个使用场景是System.Windows.Documents.Hyperlink嵌套的System.Windows.Controls.TextBlock:
<Hyperlink><TextBlock/></Hyperlink>
Run Code Online (Sandbox Code Playgroud)
另一个很简单TextBlock:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
Run Code Online (Sandbox Code Playgroud)
当然,我可以设计两个TextBlock元素:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
<Hyperlink><TextBlock Style="{StaticResource Coloring}"/></Hyperlink>
Run Code Online (Sandbox Code Playgroud)
但这无法确定Hyperlink案例的下划线风格.
如果我尝试两种类型的样式:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
<Hyperlink Style="{StaticResource Coloring}"><TextBlock/></Hyperlink>
Run Code Online (Sandbox Code Playgroud)
然后样式失败,因为(显然)没有共同的祖先类型用于TargetType …
我已经构建了一个UserControl,它以有趣和有用的方式扩展了ComboBox的功能.当它下降时看起来像这样:

我已经在控件中构建了一大堆功能,它们都能顺利运行.这让我相信我对于我正在做的事情有一些线索.你认为让UserControl的样式设置可编辑的TextBox的背景画笔是一件小事.事实上,这似乎是不可能的.我很困惑.
UserControl的XAML,非常简略(你会为此感谢我),看起来像这样:
<UserControl x:Class="MyApp.CodeLookupBox" x:Name="MainControl">
<UserControl.Resources>
<!-- tons of DataTemplates and Styles, most notably the style that
contains the control template for the ComboBox -->
<UserControl.Resources>
<ComboBox x:Name="ComboBox"
Margin="0"
Style="{DynamicResource ComboBoxStyle1}"
VerticalAlignment="Top"
ItemTemplate="{StaticResource GridViewStyleTemplate}"/>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
在这个控件中有很多代码隐藏,主要是依赖属性,我用它来选择下拉列表中使用的模板.
让我疯狂的是可编辑的文本框.我希望能够从用户控件的样式设置其背景画笔 - 例如,当我在我的XAML中声明其中一个用户控件时,它使用如下样式:
<Style TargetType="{x:Type local:CodeLookupBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsRequired}" Value="True">
<Setter Property="EditableTextBoxBackground" Value="{StaticResource RequiredFieldBrush}"/>
</DataTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
我开始简单地设置UserControl的背景,但是设置了可编辑TextBox 背后的背景.TextBox本身仍然是白色的.
在ComboBox的模板中,有一个控制TextBox的样式:
<Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="MinWidth" Value="0"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/> …Run Code Online (Sandbox Code Playgroud) WPF的初学者总是跟我一起.
我在构建一个概念验证应用程序之前继续使用它.我已经决定给它一个WPF.
这是场景:
我有:
鉴于我有一系列产品和我购买的每个产品,我需要在userControl中将tENDBox APPEND TEXT打印出所有发生的动作,textBox应该看起来像
Run Code Online (Sandbox Code Playgroud)Start buying ..... Product 1 Sold Product 2 Sold Fineshed....
我的问题是,尽管我在每件商品售出后成功通知我似乎无法看到我如何绑定或制作"产品1已售出,产品2出现在文本框中".
在Windows窗体中,我将使用一个名为ProductStatus的属性的userControl,每当收到通知我将appendText添加到textBox.来自服务时"我把beginInvoke放在了获取threadCross操作的地方".这是我要调查的另一个问题
private ProductStatus _productStatus;
public ProductStatus Status
{
get { return _productStatus; }
set
{
_printStatus = value;
BeginInvoke((MethodInvoker)(() => txtProductResult.AppendText(string.Format("Product Sold {0}", Environment.NewLine))));
}
}
Run Code Online (Sandbox Code Playgroud)
如何为myTextBox为我销售的每件商品附加文字?
=================添加了当前代码============================== =
void LogChanged(object sender, NotifyCollectionChangedEventArgs e)
{
foreach(object item in e.NewItems)
{
txtProduct.AppendText(item.ToString());
}
}
<TextBox Margin="12,41,12,59" Name="txtPrintResult" />
<TextBox Text="{Binding TicketPrintLog, Mode=OneWay}" Margin="12,41,12,12" …Run Code Online (Sandbox Code Playgroud) 我正在尝试TreeView使用数据模板将集合绑定到wpf 控件.集合中的每个项目(人物)还包含两种不同的汽车和书籍集合(汽车,书籍).
这是节省空间所涉及的对象的简化列表.
public class Person
{
public string Name
public List<Book> Books;
public List<Car> Cars;
}
public class Book
{
public string Title
public string Author
}
public class Car
{
public string Manufacturer;
public string Model;
}
Run Code Online (Sandbox Code Playgroud)
这就是我的约束力
public MainWindow()
{
InitializeComponent();
this.treeView1.ItemsSource = this.PersonList();
}
public List<Person> PersonList()
{
List<Person> list = new List<Person>();
Book eco = new Book { Title = "Economics 101", Author = "Adam Smith"};
Book design = new Book { Title …Run Code Online (Sandbox Code Playgroud) 在自定义WPF控件中,我想将控件的宽度设置为高度的函数.例如: Width = Height / 3 * x;
实现这一目标的最佳方法是什么,以便控件正确且流畅地调整大小(以及最初的大小)?
wpf ×10
textbox ×3
data-binding ×2
styles ×2
textblock ×2
width ×2
animation ×1
autosize ×1
binding ×1
c# ×1
controls ×1
height ×1
mvvm ×1
storyboard ×1
templates ×1
treeview ×1
validation ×1
wpf-controls ×1
xaml ×1