我有一个文本框,其Text属性具有TwoWay MultiBinding,UpdateSourceTrigger设置为PropertyChanged.第一个Binding是一个依赖属性(Value),它有一个PropertyChangedCallBack函数,可以将值四舍五入到一个小数位.
文本框的目的是在用户键入时执行舍入,而不是在文本框失去焦点时执行舍入,因此将UpdateSourceTrigger设置为PropertyChanged.
我遇到的问题是,如果输入的文本不会导致值更改,则Text属性和Value将变得不同步.仅当舍入操作导致值更改时,文本才会动态更新.例如,如果Text和Value都是123.4并且用户在此之后键入1,则Value将四舍五入为相同的值(123.4),但Text显示为123.41.但是,如果在4之后键入9,则值向上舍入为123.5.由于这个实际的变化,文本然后更新为相同(123.5).
是否有任何方法强制文本框从其源更新,即使源自上次触发后没有更改?我尝试过使用BindingExpressionBase.UpdateTarget()但这仅在UpdateSourceTrigger设置为Explicit时有效,不能用作在可以调用UpdateTarget的合适时间之前不再更新的值(例如TextInput处理程序).我已经尝试过其他方法,例如从绑定值显式更新Text值,暂时强制实际更改为Value以调用更新,但这些"hacks"要么不起作用,要么导致其他问题.
任何帮助将不胜感激.
代码如下.
<TextBox>
<TextBox.Text>
<MultiBinding Converter="{local:NumberFormatConverter}"
UpdateSourceTrigger="Explicit"
Mode="TwoWay">
<Binding Path="Value"
RelativeSource="{RelativeSource AncestorType={x:Type Window}}"
Mode="TwoWay" />
</MultiBinding> …Run Code Online (Sandbox Code Playgroud) binding textbox updatesourcetrigger propertychanged multibinding
有没有办法改变绑定的默认行为,所以我不需要在每个文件框中设置'UpdateSourceTrigger = PropertyChanged'?
这可以通过ControlTemplate或Style完成吗?
我有以下代码:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Text="{Binding Path=Name,
Mode=OneWayToSource,
UpdateSourceTrigger=Explicit,
FallbackValue=default text}"
KeyUp="TextBox_KeyUp"
x:Name="textBox1"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BindingExpression exp = this.textBox1.GetBindingExpression(TextBox.TextProperty);
exp.UpdateSource();
}
}
}
public class ViewModel
{
public string Name
{
set
{
Debug.WriteLine("setting name: " + value);
}
}
}
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs …Run Code Online (Sandbox Code Playgroud) 我看到了这个例子--Binding.UpdateSourceTrigger属性
在示例中,UpdateSourceTrigger设置为Explicit,然后在视图代码中调用TextBox名称的UpdateSource.
但是,如果我使用MVVM dp,我不想让我的控件和源属性在VM中,而不是在视图中,那么将控件绑定到VM属性并将UpdateSourceTrigger设置为显式的正确方法是什么?
我想这样做,因为在我的情况下它的ShowDialog窗口,我希望只有当用户点击"确定"时源才会更新
提前致谢!
我有一个Silverlight 2应用程序验证数据OnTabSelectionChanged.我立即开始希望UpdateSourceTrigger不仅允许LostFocus,因为如果单击选项卡而不选择控件,则在验证之前不会更新LINQ对象.
我通过将焦点设置为另一个控件然后返回OnTextChanged解决了TextBoxes的问题:
Private Sub OnTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
txtSetFocus.Focus()
sender.Focus()
End Sub
Run Code Online (Sandbox Code Playgroud)
现在我试图在DataGrid中完成同样的黑客攻击.我的DataGrid使用在运行时为CellTemplate和CellEditingTemplate生成的DataTemplates.我尝试将TextChanged ="OnTextChanged"写入DataTemplate中的TextBox,但不会触发它.
有人有主意吗?
我正在使用WPF Toolkit DataGrid和DataGridComboBoxColumn.一切都运行良好,除了在组合框中发生选择更改时,所选的值绑定源不会立即更新.仅当组合框失去焦点时才会发生这种情况.有没有人遇到过这个问题和任何建议解决方案?
这是该列的xaml:
<toolkit:DataGridComboBoxColumn Header="Column" SelectedValueBinding="{Binding Path=Params.ColumnName, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="cName"
SelectedValuePath="cName">
<toolkit:DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Info.Columns}" />
</Style>
</toolkit:DataGridComboBoxColumn.ElementStyle>
<toolkit:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Info.Columns}" />
</Style>
</toolkit:DataGridComboBoxColumn.EditingElementStyle>
</toolkit:DataGridComboBoxColumn>
Run Code Online (Sandbox Code Playgroud) 我正在寻找创建一个自定义版本的UpdateSourceTrigger,我可以使用我的绑定.我不知道这是否可行,或者相反,我需要创建自己的绑定类.我正在寻找的是,而不是LostFocus或PropertyChanged,它有一些东西,它将在一些指定的时间限制后更新源.
我发现了这个,但我不知道是否有更好的方法(其中一条评论提到了一些内存泄漏与实现).
有任何想法吗?
我正在使用 MVVM 并遇到以下问题。我的 TextBox.Text 与 UpdateSourceTrigger=LostFocus 绑定(这就是用户想要的)。我有一个带有 SaveCommand CommandBinding 的按钮 - 这有效。现在我有一个带有 Strg+S 的 KeyBinding,它也执行 SaveCommand。这就是问题所在:当我在文本框中并按 Strg+s 时,更改不在视图模型中。
有没有办法让 MVVM 命令与 KeyBinding 和 TextBox UpdateSourceTrigger=LostFocus 一起工作?
一些代码来检查问题
<Window>
<Window.InputBindings>
<KeyBinding Key="S" Modifiers="Control" Command="{Binding SaveCommand}"></KeyBinding>
</Window.InputBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="{Binding MyText1, UpdateSourceTrigger=LostFocus}" Width="100"></TextBox>
<Button Grid.Row="1" Content="_Save" Command="{Binding SaveCommand}" IsDefault="True"></Button>
</Grid>
</Window>
public partial class MainWindow : Window
{
private Viewmodel _data;
public MainWindow()
{
_data = new Viewmodel();
InitializeComponent();
this.DataContext = _data;
}
}
public …Run Code Online (Sandbox Code Playgroud) 我有一个包含来自设置对象的信息的 DataGrid。目前,DataGrid 和设置对象之间存在双向绑定。但是,我想在其中放置一个“保存”按钮,如果用户单击“保存”按钮,它只会将 DataGrid 中所做的更改绑定到对象。但是,我不确定如何使用 DataGrid 为我的特定情况调用 UpdateSource()。
这是我的 xaml.cs 代码:
public void LoadDataFields(Data d)
{
Grid1.ItemsSource = d.Fields;
}
private void SaveChanges(object sender, RoutedEventArgs e)
{
BindingExpression be = Grid1.GetBindingExpression(DataGrid.ItemsSourceProperty);
be.UpdateSource();
}
Run Code Online (Sandbox Code Playgroud)
这是我的 xaml 代码:
<DataGrid x:Name="Grid1"
IsReadOnly="False"
Height="360"
Margin="20,15,20,15"
VerticalAlignment="Top"
AutoGenerateColumns="False"
CanUserAddRows="False" SelectionUnit="Cell"
ItemsSource="{Binding data}"
>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=name, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Length of Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=length, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以调用 UpdateSource() 以便仅在单击“保存”按钮时才进行绑定?我的猜测是我只是为 …
我有一个组合框(在我的wpf-mvvm应用程序中).我已经定了IsEditable = true.但是当我开始打字时,"财产改变事件"就被解雇了.
我怎么能UpdateSourceTrigger = Propertychanged在这里设置?
另外..如果用户输入了新值,我需要调用验证函数(我的意思是除了列表中可用的那些...使用编辑功能).
任何帮助将不胜感激.
<ComboBox ItemsSource="{Binding Path = PlanTypeBasedContractNumberList }" Width="90" IsEditable="True"
SelectedValue="{Binding GeneralCharacteristicsDataContext.ContractNumber.Value}">
</ComboBox>
Run Code Online (Sandbox Code Playgroud) 我相信当你输入一个你喜欢的文本框时,大多数人都会遇到这种情况,根据已输入的内容过滤ViewCollection.
它在WPF中非常直接,只需将UpdateSourceTrigger更改为Text绑定到PropertyChanged即可.
正如您所料,Silverlight没有它.只有默认和显式.
我有想法将交互行为绑定到文本框的TextChanged事件.你会推荐这种方法还是有更好的方法?
silverlight updatesourcetrigger propertychanged silverlight-4.0
我的应用程序中有一个文本框,它是绑定到我的类中的十进制字段的数据,绑定模式是双向的.我使用StringFormat = {0:c}进行货币格式化.
只要我不触摸'UpdateSourceTrigger',这就可以正常工作.如果我设置UpdateSourceTrigger = PropertyChanged,它将停止格式化我输入的文本.
这是我的代码示例
Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Converter
{
public class Employee : INotifyPropertyChanged
{
int _employeeNumber;
string _firstName;
string _lastName;
string _department;
string _title;
decimal _salary;
public Employee()
{
_employeeNumber = 0;
_firstName =
_lastName =
_department =
_title = null;
}
public int EmployeeNumber
{
get { return _employeeNumber; }
set
{
_employeeNumber = value;
OnPropertyChanged("EmployeeNumber");
}
}
public string FirstName
{
get { return _firstName; …Run Code Online (Sandbox Code Playgroud) 只是一个简短的问题:
在wpf中,如何在代码隐藏中设置和获取文本框的updatesourcetrigger?
谢谢
更新:
我遵循AngleWPF的代码:
var bndExp = BindingOperations.GetBindingExpression(this, TextBox.TextProperty);
var myBinding
= bndExp.ParentBinding;
var updateSourceTrigger = myBinding.UpdateSourceTrigger;
Run Code Online (Sandbox Code Playgroud)
但我得到了例外:
PresentationFramework.dll中出现未处理的"System.Reflection.TargetInvocationException"类型的异常附加信息:调用目标已抛出异常.
wpf ×10
binding ×4
datagrid ×3
mvvm ×3
c# ×2
combobox ×2
data-binding ×2
silverlight ×2
textbox ×2
.net-4.0 ×1
converter ×1
explicit ×1
key-bindings ×1
multibinding ×1
showdialog ×1
wpftoolkit ×1
xaml ×1