我有一个网格,位于名为 MediatedUserControl 的用户控件派生类内。我添加了一个上下文菜单来让用户删除一个项目,但我一直无法弄清楚如何将命令绑定到我的命令属性。我正在使用 MVVM,并且我的视图模型实现了一个名为 DeleteSelectedItemCommand 的公共 ICommand 属性。
但是,当显示视图时,我在输出窗口中收到以下消息:
System.Windows.Data 错误:4:无法找到引用“RelativeSource FindAncestor、AncestorType='BRO.View.MediatedUserControl”、AncestorLevel='1'' 进行绑定的源。BindingExpression:Path=DataContext.DeleteSelectedItemCommand; 数据项=空;目标元素是“BarButtonItem”(HashCode=6860584);目标属性是“Command”(类型“ICommand”)
我觉得我通常可以很好地处理这样的绑定,但无法弄清楚我在这里缺少什么。感谢您的任何帮助,您可以提供。
<dxg:GridControl HorizontalAlignment="Left" Margin="12,88,0,0" x:Name="gridControl1" VerticalAlignment="Top" Height="500" Width="517" DataSource="{Binding ItemList}" BorderBrush="{StaticResource {x:Static SystemColors.ActiveBorderBrushKey}}" ShowBorder="True" Background="{StaticResource {x:Static SystemColors.ControlLightBrushKey}}" UseLayoutRounding="False" DataContext="{Binding}">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Code" Header="Code" Width="107" />
<dxg:GridColumn FieldName="Name" Header="Item" Width="173" />
<dxg:GridColumn FieldName="PricePerItem" Header="Unit Price" Width="70">
<dxg:GridColumn.EditSettings>
<dxe:TextEditSettings DisplayFormat="N2" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Quantity" Header="Qty" Width="50" AllowEditing="True" />
<dxg:GridColumn FieldName="TotalPrice" Header="Total Price" Width="90">
<dxg:GridColumn.EditSettings>
<dxe:TextEditSettings DisplayFormat="N2" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView ShowIndicator="False" ShowGroupPanel="False" MultiSelectMode="Row" AllowColumnFiltering="False" AllowBestFit="False" …Run Code Online (Sandbox Code Playgroud) 我在业务层中有一些逻辑根据输入限制 ComboBox 选项,因此我需要更改底层 BindingList 中的值。但是,当列表发生更改时,双向绑定将变为仅从 UI 到实体的单向绑定。
_mComboBox.DataBindings.Add("SelectedValue", _mEntity, "WifeCount");
Run Code Online (Sandbox Code Playgroud)
分配按钮单击处理程序中存在问题的完整代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EnumDataBinding
{
public partial class Form1 : Form
{
ComboBox _mComboBox = new ComboBox();
Button _mCheckButton = new Button();
Button _mAssignButton = new Button();
BindingList<OptionValue> _mBindingList = new BindingList<OptionValue>();
List<OptionValue> _mCacheList = new List<OptionValue>();
Entity _mEntity = new Entity();
public Form1()
{
InitializeComponent();
// create a reset button
_mCheckButton.Size = …Run Code Online (Sandbox Code Playgroud) 预期行为
我如何尝试实施
日期间隔和日期选择器的 XAML 代码
<ComboBox ItemsSource="{Binding Source={StaticResource viewByInterval}}"
SelectedValuePath="Value"
SelectedItem="{Binding IntervalMode,Mode=TwoWay}" />
<DatePicker SelectedDate="{Binding EndDate,Mode=TwoWay}"
IsEnabled="{Binding Path=EndDateEnabled[0],Mode=TwoWay}">
Run Code Online (Sandbox Code Playgroud)
查看用于更改 EndDateEnabled 的模型代码
public bool EndDateEnabled { get; set; }
public DateMode IntervalMode
{
get
{
return _dateModeValue;
}
set
{
_dateModeValue = value;
EndDateEnabled = (value == DateMode.CustomDateRange);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法实现该功能。请指教。
我正在 WPF/MVVM 中开发一个标准数据输入应用程序。我有一个名为 LabSetupView.xaml 的文件,其中有三个控件:
这些控件旨在协同工作 - 当用户选择技术人员时,仅显示该技术人员 ID 的测试。这是通过技术人员到测试的导航属性完成的。
我正在考虑将这些控件分别作为 a 移动到它们自己的 .xaml 文件中<UserControl>,然后将它们嵌套在 中,<Window>因为 LabSetupView.xaml 的 DataContext 只能设置一次。因此,我想如果我将控件组件化,我可以为每个控件创建 ViewModel(即 LabView.xaml/LabViewModel.xaml 等...),并且每个控件都继承自实现必要的INotifyPropertyChanged.
我的问题是:拥有一个本身没有数据上下文但包含单独的 UserControls 的底板 .xaml 文件是否是一种常见或正常的做法,每个 UserControls 都有自己的数据上下文?
我问这个问题是因为我担心如果我尝试这样的事情,那么数据绑定和 INotifyProperChanged 将无法一起工作,因为这些视图是组件化的。换句话说,我上面的用例(为技术人员获取测试)不会绑定或更新属性。
我有一个切换按钮,它有两个绑定到按钮 IsChecked 属性的数据触发器。在数据触发器中,我设置按钮内容及其命令属性。我遇到的问题是,当您单击按钮时,数据触发器正在更改命令值,然后触发命令。是否可以先命令触发,然后再触发数据?这是代码。
<ToggleButton x:Name="commandToggleButton" Grid.Row="0" Height="30" HorizontalAlignment="Left" Margin="26,24,0,0" VerticalAlignment="Top" Width="75">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=commandToggleButton, Path=IsChecked}" Value="False">
<Setter Property="Content" Value="Build" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Command" Value="{Binding Build}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=commandToggleButton, Path=IsChecked}" Value="True">
<Setter Property="Content" Value="Cancel" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Command" Value="{Binding CancelBuild}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
Run Code Online (Sandbox Code Playgroud)
我希望发生的是,当第一次单击按钮时将触发 Build 命令,然后第二次单击按钮时将触发 CancelBuild 命令。
在一个简单的 MVVM 应用程序中,我可以添加对视图中某些属性的绑定。该属性可以访问另一个对象并从中返回属性值。假设我想在视图中显示活动项目。如果没有项目处于活动状态,则应显示特殊注释。
现在,当项目重命名时,名称应该在视图中更新。如果我只是在属性中返回项目名称,它当然不会更新。
所以我想我可以将视图绑定到属性中创建的另一个绑定,该绑定应该转发 PropertyChanged 事件并相应地更新视图。但我看到“System.Windows.Data.Binding”,而不是像“Project: XYZ”这样的绑定的预期结果。
该项目可以在任何地方重命名,因此我想避免从那里自己引发此 ViewModel 的 PropertyChanged 事件。事情本身应该更聪明一点,而不需要从各处推动(当事情变得更加复杂时,你经常会忘记至少一次)。
这是代码:
XAML 视图:
<TextBlock Text="{Binding ActiveProjectName}"/>
Run Code Online (Sandbox Code Playgroud)
C# 视图模型:
public object ActiveProjectName
{
get
{
if (ActiveProject != null)
{
// This works but won't update automatically:
//return "Project: " + ActiveProject.Name;
// This does not work at all:
return new Binding("Name")
{
Source = ActiveProject,
StringFormat = "Project: {0}"
};
}
return "(No active project)";
}
}
Run Code Online (Sandbox Code Playgroud)
这有可能吗?它是如何正确运作的?
我有以下 XAML:
<Window x:Class="ListBoxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListBoxTest"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:Model />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding Items}" Grid.Row="0"/>
<Button Content="Add" Click="Button_Click" Grid.Row="1" Margin="5"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
以及该类的以下代码Model,将其放入主窗口中DataContext:
public class Model : INotifyPropertyChanged
{
public Model()
{
items = new Dictionary<int, string>();
}
public void AddItem()
{
items.Add(items.Count, items.Count.ToString());
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Items"));
}
private Dictionary<int, string> items;
public IEnumerable<string> Items { get { return items.Values; } } …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我通过 xaml 中的数据模板分配每个视图数据上下文。
问题是我现在有一个作为单例实现的视图模型,并且需要有两个视图使用它。
有没有一种方法可以在一个数据模板中设置多个视图?
我知道像下面这样的东西是行不通的,我只是用这个作为我的意思的例子:
<DataTemplate DataType="{x:Type viewModel:ThisViewModel}">
<view:ViewOne />
<view:ViewTwo />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个像这样在 xml 中定义的微调器
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/expense_category"
app:sourceData="@{()->createExpenseViewModel.getAllSourceItems(1)}"
app:layout_constraintStart_toStartOf="@+id/textView"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintWidth_percent="0.7"
/>
Run Code Online (Sandbox Code Playgroud)
createExpenseViewModel.getAllSourceItems(1) 这个方法返回 LiveData <List<Source>>,所以我为这种情况编写了一个绑定适配器
@BindingAdapter("app:sourceData")
fun setSourceData(spinner: Spinner, sourceList: List<Source>) {
val categoryItems = ArrayList<String>()
categoryItems.addAll(sourceList.map { it.sourceName })
val spinnerAdapter =
ArrayAdapter<String>(spinner.context, R.layout.simple_spinner_dropdown_item, categoryItems)
spinner.adapter = spinnerAdapter
}
Run Code Online (Sandbox Code Playgroud)
构建应用程序时,我收到以下错误,
****/ data binding error ****msg:Cannot find the proper callback class for app:sourceData. Tried java.util.List but it has 25 abstract methods, should have 1 abstract methods. file:/home/naveen/Desktop/project-expense/app/src/main/res/layout/activity_create_expense.xml loc:94:34 - 94:80 ****\ data binding error ****
这个错误实际上是什么意思,如何解决这个错误?
编辑: …
我正在通过添加启用数据绑定
数据绑定 { 启用 = 真 }
和
kapt 'com.android.databinding:compiler:3.1.4'
在应用程序级build.gradle文件中。
应用插件:'kotlin-kapt'
最重要的是。项目基于 kotlin 。
这是我的模型:
package ir.app.myapplication;
data class cisclass(val equRevId:String)
Run Code Online (Sandbox Code Playgroud)
主要活动 :
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val user = cisclass("123")
binding.setVariable(BR.cis, user)
binding.executePendingBindings()
Run Code Online (Sandbox Code Playgroud)
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<data>
<variable
name="cis"
type="ir.app.myapplication.cisclass" />
</data>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="@{cis.equRevId}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</layout>
Run Code Online (Sandbox Code Playgroud)
我会得到这个错误:
java.lang.IllegalArgumentException: couldn't make a guess for ir.meedc.myapplication.cisclass …Run Code Online (Sandbox Code Playgroud) data-binding ×10
c# ×6
wpf ×6
mvvm ×3
android ×2
kotlin ×2
xaml ×2
.net ×1
binding ×1
combobox ×1
datacontext ×1
datatrigger ×1
winforms ×1
wpf-controls ×1