Tag: 数据绑定

更改属性时,C#XAML数据绑定无效

好吧,我一直在为这个问题大肆渲染我的脑子,我错过了一些东西,我无法弄清楚是什么。 最终我正在尝试设置数据绑定,以便我可以更新值,以便在运行中显示,但对于我的生活,它不起作用。 XAML是: 请注意,我设置了{Binding Path=TBBind} 。 背后的代码是: using System.ComponentModel; using System.Windows; namespace Databinding_Practice_2 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); TBBind = “test”; } private string _tBBind; public string TBBind { get { return _tBBind; } set { if (value != _tBBind) […]

如何使用C#xaml以编程方式设置数据绑定

如何以编程方式设置DataContext并在C#Xaml中创建数据绑定? 鉴于一个类 class Boat : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; internal void OnPropertyChanged(String info) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(info)); } } private int width; public int Width { get { return this.width; } set { this.width = value; OnPropertyChanged(“Width”); } } } 我试图以编程方式使用数据绑定设置Xaml矩形的宽度。 Boat theBoat = […]

使用EF5类将DataGridView绑定到数据库而不将更改发送到数据库

我正在使用Entity Framework 5.0 + .NET 4.5 我使用EF Model第一种方法创建了数据库,我想使用EF类将DataGridView绑定到数据库,以便自动同步对数据库或DataGridView任何更改。 这是我的代码: //form level fields private BindingList _products; private BindingSource _productSource = new BindingSource(); … in the form load event //load the data from database using EF classes var tmp = _context.BaseCategorySet.OfType().ToList(); //converting to IBindingList _products = new BindingList(tmp); _products.AllowEdit = true; _products.AllowNew = true; _productSource.DataSource = _products; […]

如何在C#后面的代码中动态创建数据模板并绑定树视图层次结构数据

我有一个场景,树视图动态地更改其数据模板和数据绑定定义。 我在XAML中创建了一个树视图,如下所示: 我没有在XAML中定义数据模板和绑定定义。 因为必须根据用户的偏好更改数据模板和绑定定义。 我尝试了以下在此处找到的C#代码,以动态创建数据模板定义。 但是,看下面的代码,我无法弄清楚如何通过C#代码更改数据绑定定义 。 private DataTemplate GetHeaderTemplate() { //create the data template DataTemplate dataTemplate = new DataTemplate(); //create stack pane; FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel)); stackPanel.Name = “parentStackpanel”; stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); // Create check box FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox)); checkBox.Name = “chk”; checkBox.SetValue(CheckBox.NameProperty, “chk”); checkBox.SetValue(CheckBox.TagProperty, new Binding()); checkBox.SetValue(CheckBox.MarginProperty, new Thickness(2)); checkBox.SetValue(CheckBox.TagProperty, new […]

在自定义控件上进行OneWayToSource / TwoWay绑定

我有一个带依赖项属性的自定义控件,定义如下: public class TemplatedTextBox : TextBox { public static readonly DependencyProperty SearchStringProperty = DependencyProperty.Register(“SearchString”, typeof(string), typeof(TemplatedTextBox), new UIPropertyMetadata(string.Empty)); public string SearchString { get { return (string)GetValue(SearchStringProperty); } set { SetValue(SearchStringProperty, value); } } } 我使用以下控件模板: 我想在OneWayToSource或TwoWay绑定模式下将SearchTextBox的Text属性绑定到我的SearchString属性。 我试过了: Text=”{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SearchString, Mode=OneWayToSource}” 哪个什么都不做。 Text=”{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SearchString, Mode=TwoWay}” 和 Text=”{TemplateBinding SearchString}” 当我以编程方式更改SearchString TextBox上的Text更改时,哪个方向可以正常工作,但不是另一种方式 我也尝试将SearchString作为常规属性,并在各种Mode使用RelativeSource绑定它,但它不起作用。 在常规的View-to-ViewModel绑定中,这是一个非常直接的事情,所以我在这里缺少什么?

使用DependencyProperty进行可见性绑定

我在下面的一些简单代码中使用了ToggleButton.IsChecked属性来设置TextBlock的可见性。 它工作正常。 由于这不适合我的程序结构,我试图将另一个TextBlock的可见性绑定到“this”的DependencyProperty。 它编译很好,但它没有产生任何影响。 我做错了什么,只是不确定是什么。 XAML C# using System.Windows; namespace ToggleButtonTest { public partial class MainWindow : Window { static MainWindow() { FrameworkPropertyMetadata meta = new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault); ShowMoreTextProperty = DependencyProperty.Register(“ShowMoreText”, typeof(bool), typeof(MainWindow), meta); } public MainWindow() { InitializeComponent(); } public static readonly DependencyProperty ShowMoreTextProperty; public bool ShowMoreText { get { return (bool)GetValue(ShowMoreTextProperty); } set […]

如何使用具有Reactive UI的Xamarin Android将数据绑定到自定义ListView

我正在使用具有Reactive UI的Xamarin Android而不使用Xamarin Forms。 我有一个自定义ListView(我已将其布局定义为xaml)。 我不知道如何使用活动类中的OneWayBind方法将此控件绑定到ViewModel中的observableCollection 。 我写的是, this.OneWayBind(ViewModel, x => x.OutletListing, x => x.List).DisposeWith(SubscriptionDisposables); 但是给了, System.ArgumentException:无法将System.Collections.ObjectModel.ObservableCollection1转换为Android.Widget.ListView。 要解决此问题,请注册IBindingTypeConverter 我在教程中看到Xamarin Forms已经使用了ItemSource属性。 任何人都可以为此提供解决方案。 提前致谢。 更新我不知道如何继续给出答案。 我想更多地了解这一点。 这是我的ViewModel类。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using ReactiveUI; using System.Collections.ObjectModel; using System.Reactive.Linq; using Splat; […]

C#WPF绑定到索引属性 – 我做错了什么?

我最近发现了索引属性。 这看起来像是我正在使用的数据最好在集合中表示的场景的完美解决方案,但仍然需要实现为可以在XAML数据绑定中使用的属性。 我开始只是测试创建索引属性,我没有遇到任何问题,但我似乎无法使绑定工作。 谁能指出我哪里出错? 这是带有嵌套类的测试类,用于创建索引属性: public class TestListProperty { public readonly ListProperty ListData; //————————— public class ListProperty : INotifyPropertyChanged { private List m_data; internal ListProperty() { m_data = new List(); } public string this[int index] { get { if ( m_data.Count > index ) { return m_data[index]; } else { return “Element not set for […]

调试/断点为什么WPF BindingExpression为null

我有一个WPF CustomControl,带有一个名为SelectedRange的depdendency属性。 在XAML中的应用程序中,绑定设置如下: SelectedRange=”{Binding Source={StaticResource ResourceKey=MainWindow}, Path=GlobalXRange, Mode=TwoWay}” 我在代码中发现BindingExpression被覆盖(设置为null)。 我可以用这段代码确定这个: public IRange SelectedRange { get { return (IRange)GetValue(SelectedRangeProperty); } set { SetValue(SelectedRangeProperty, value); Binding b = BindingOperations.GetBinding(this, SelectedRangeProperty); BindingExpression be = BindingOperations.GetBindingExpression(this, SelectedRangeProperty); if (be == null) { Console.WriteLine(“Binding expression is null!”); } } } 事实certificate,应用程序启动后BindingExpression为null。 所以我的问题是 – 如何调试为什么这个BindingExpression为空 – 例如它在XAML中设置如何找出它被设置为null的位置? 注意:输出控制台中没有绑定错误

在数据绑定控件中应用条件格式时的最佳做法?

我有一个自定义对象(一个EntitySpaces查询)绑定的Repeater控件,并注意到有几种方法可以有条件地格式化显示的值。 1)从我的aspx我可以在我的代码隐藏中调用一个方法并传递绑定值并使用它来驱动任何条件逻辑: <a class="star" href="https://stackoverflow.com/questions/405480/best-practices-when-applying-conditional-formatting-in-data-bound-controls/”> and then in the code-dehind: protected string MakePackageSelectionUrl(int packageId) { return string.Format(“/Packages/NoAjax/ToggleStar.aspx?p={0}&s={1}&st={2}”, packageId, _streamId, (int)_phase); } 2)我可以挂钩到ItemDataBound事件,将e.Item.DataItem检索为DataRowView,然后发疯: protected void PackageList_ItemDataBound(Object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) { return; } DataRowView details = (DataRowView)e.Item.DataItem; EncodePackageName(e, details); EncodeStatusName(e); DisplayStarImage(e, details); } private static void EncodePackageName(RepeaterItemEventArgs e, […]