Tag: 绑定

WPF ComboBox绑定未正确更新

我的ComboBox中有Binding的更新问题。 我创建了一个简化的例子来说明这个问题。 我将在下面粘贴所有相关代码。 所以在上图中,我有一个TextBlock(黑色),显示SelectedPerson的’SelectedPerson.FullName’属性。 SelectedPerson的FirstName和LastName属性显示在FullName下面的2个TextBox中。 ComboBox DisplayMemberPath绑定到“FullName”,combobox包含Person对象列表。 “FullName”属性在TextBlock,ComboBox和ComboBox下拉列表中应该相同。 但是,它只能在TextBlock中正确更新。 comboBox Dropdown只是在第一次打开时更新,之后不会更新。 因此,如果我在文本框中编辑FirstName并单击下拉列表然后再编辑它并再次单击下拉列表,我们最终会为SelectedPerson显示3个不同的“FullName”值。 我在后面的代码中使用INotifyPropertyChanged来通知“SelectedPerson”的更改。 这似乎可以很好地更新TextBlock,但由于某种原因,ComboBox没有使用新的“FullName”进行更新。 当我发送’SelectedPerson’通知时,我试图发送一个“FullName”通知,但它也没有使它工作。 任何人都能告诉我为什么在更改FirstName文本时ComboBox没有更新? 谢谢。 MainWindow.xaml: MainWindow.xaml.cs: using System.Collections.Generic; using System.ComponentModel; namespace ComboBoxBinding { public partial class MainWindow : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private List _people […]

如何绑定到WPF中的另一个控件属性

我通过命名空间使用WPF Charting ToolKit: xmlns:ChartingToolkit=”clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit” xmlns:VisualizationToolkit=”clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit” 我有一个图表控件,在每次进行Lineseries绘图时我会生成一个随机颜色。 我删除数据点标记并使用以下样式着色 LineSeries是通过定义的 现在,这可以正常工作但图例标记显示的颜色与我为LineSeries生成的随机颜色不同。 我想为LineSeries和Lineseries本身的图例项目显示相同的颜色。 所以,我将传说项目设置如下 我的问题是如何“绑定” TestSuiteLegendItemStyle样式中的Rectangle.Fill ,以便它与LineDataPointStyle定义的LineDataPointStyle设置的LineSeries颜色相同? * 谢谢你的时间。 编辑。 我已经尝试设置一个DependencyProperty ,它保存我的情节的Background颜色,如下所示 … … 我已经修改了转换器(标记为),以便我可以存储随机生成的背景颜色,然后在我的图例中使用它 public class ColorToBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Brush b = new SolidColorBrush(Utils.GenerateRandomColor()); MultiChartExtensions.BackgroundBrushProperty = b; <= how to set the dependency property? return […]

如何将numericUpDown双向绑定到成员类

我需要twoway绑定configClass.RaMsize到numericUpDown。 BindField(this.upDownRamSize, “Value”, configClass, “RaMsize”);//all right this.upDownRamSize.Value = 1213;// configClass.RaMsize – not change – it’s bad! 方法: public static void BindField(Control control, string propertyName, object dataSource, string dataMember) { Binding bd; for (int index = control.DataBindings.Count – 1; (index == 0); index–) { bd = control.DataBindings[index]; if (bd.PropertyName == propertyName) control.DataBindings.Remove(bd); } control.DataBindings.Add(propertyName, dataSource, dataMember); […]

WPF数据绑定到更改对象

我有一个用户控件,显示当前登录的用户名。 我已将控件中的TextBlock绑定到我的应用程序中的User obejct的UserId属性。 我遇到的问题是,每次新用户登录时,我的绑定作为源的用户对象都会更改。 我可以想到一个解决方案,当用户obejct发生变化时触发一个事件,然后通过控制抓住我,然后重新初始化绑定,但这似乎不太理想。 有这个问题的解决方案,我觉得它一定很常见吗? 干杯, 詹姆士

绑定到后面的代码中的relativesource

在我的UserControl中,我在XAML中有以下代码 这只是从父窗口获取属性的值,它工作得很好。 我怎么能在后面的代码中执行此操作? Binding b = new Binding(); b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1); b.Path = “StartTime”; myProperty = b.value;// obviously there is no b.value but this // is what I’m trying to achieve. //BindingOperations.SetBinding(StartTime, StartTimeProperty, b); //This does not work sadly, it can’t convert string to DependancyObject

WPF双向绑定XML

我很难掌握WPF,更具体地说是执行xml文件的双向绑定。 我应该使用XMLDataProvider还是他们的另一个(更好)选项? 数据显示正常,但是当我更改条目时,更改不会反映在xml文件中。 XML: DAX 01/01/2010 01/04/2010 DJI 01/07/2010 01/10/2010 XAML: CodeBehind: public MainWindow() { InitializeComponent(); } private void DataGridLic_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { XMLData.Document.Save(“XMLFile1.xml”); } 更新:xml数据绑定的一些有用资源: http://msdn.microsoft.com/en-us/library/bb669141.aspx http://msdn.microsoft.com/en-us/library/cc165615.aspx

Windows 10 x:绑定到SelectedItem

我正在尝试移植/采用我的Windows RT应用程序到WIndows10,我正在尝试新的绑定x:Bind。 到目前为止,我能够绑定到我的ViewModel属性和其他Viewelements。 但现在我正在尝试将TextBox的文本绑定到GridView的SelectedItem。 在经典绑定中,我就是这样做的。 我正在尝试 Text =“{x:Bind gv_textgroup.SelectedItem.bezeich,Mode = TwoWay}” Text =“{x:Bind textgroup [gv_textgroup.SelectedIndex] .bezeich,Mode = TwoWay}” textgroup是我的viewmodelclass,包含所有元素 但它没有奏效……任何想法? 并且有人可以解释我如何处理“DependencyProperty”。 我在“build 2015”中观看了viedo,并提供了示例代码。 但它对我说什么……我是一个新手…… 非常感谢您的帮助

Winforms ComboBox SelectedItem更改不会影响BindingSource

我正在制作C#/ WinForms应用程序。 我还无法解决的问题是,当我以编程方式更改ComboBox的SelectedItem时,它会被更改,直到ComboBox失去焦点,之后它会在分配SelectedItem之前“提醒”其值。 我认为它需要来自绑定源的旧值。 当使用UI选择项目时,底层绑定对象会正常更新,但是当我以编程方式将新值分配给SelectedItem时,情况并非如此。 只是为了获得更多信息:我正在尝试实现“撤销”,这意味着我正在保存每个更改,当编辑>>>撤消我正在撤消用户完成的所有这些更改。 有趣的是,其他控件(TextBoxes,NumericUpDowns)工作正常。 以下是详细信息: 我有一个ComboBox,它绑定到ComboItem对象,如下所示: ComboBox comboBox = new ComboBox(); List items = new List(); ComboList comboList = Configuration.ComboList.LoadComboList(); Combo myCombo = comboList.GetCombo(control.MemberName); if (myCombo != null) { items.Add(new ComboItem(0, “”, 0.0, 0.0)); for (int index = 0; index < myCombo.ComboItems.Count; index++) { items.Add(myCombo.ComboItems[index]); } } 其中Combo和ComboList是用于从配置文件加载数据的自定义类。 然后我也设置了Display和Value成员以及DataSource: comboBox.DisplayMember = “Text”; […]

如何将绑定中的FallbackValue设置为外部图像文件的路径?

我正在尝试设置FallbackValue以防万一我的转换器无法调用,但我不知道该怎么做。 转换器中外部图像的路径看起来像那样,当LatestPosition!= null时,图像以适当的方式设置。 private static readonly ImageSource Dev1 = new BitmapImage(new Uri(“/Pictures/dev1.png”, UriKind.Relative)); private static readonly ImageSource Dev2 = new BitmapImage(new Uri(“/Pictures/dev2.png”, UriKind.Relative));

如何计算WPF绑定中的值

我有一个应用程序,它使用两个滑块生成代码中其他地方使用的产品。 我想要的是将产品值绑定到文本块或工具提示,例如,看起来像“10 x 15 = 150”。 第一部分很简单,看起来像这样: 但是,将产品放在那里的简单方法是什么? 使用Pavlo Glazkov的解决方案,我将其修改为如下所示: public class MultiplyFormulaStringConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var doubleValues = values.Cast().ToArray(); double x = doubleValues[0]; double y = doubleValues[1]; var leftPart = x.ToString() + ” x ” + y.ToString(); var rightPart = (x * y).ToString(); […]