我有点绝望,因为昨天它可以工作,但今天这不再有效,让我解释一下,我正在尝试做什么:
我想设置DataSource的ComboBox所有枚举值在一个特定的枚举。就像这样:
cmbType.DataSource = m_addViewPresenter.Types;
Run Code Online (Sandbox Code Playgroud)
其中 Types 是 aBindingList并且正在像这样初始化:
public BindingList<MyEnum> Types
{
get { return m_types; }
set
{
m_types = value;
OnPropertyChanged();
}
}
[ImportingConstructor]
public AddViewPresenter()
{
Types = new BindingList<MyEnum>(
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>().ToList());
}
Run Code Online (Sandbox Code Playgroud)
此外,我想将选择的当前项目绑定到一个属性。由于ComboBox.SelectedItem不触发 -INotifyProperty事件,我使用ComboBox.SelectedValue.
cmbType.Bind(m_addViewPresenter, c => c.SelectedValue, m => m.SelectedValue);
public static void Bind<TComponent, T>(
this TComponent component, T value,
Expression<Func<TComponent, object>> controlProperty,
Expression<Func<T, object>> modelProperty)
where TComponent : IBindableComponent
where T …Run Code Online (Sandbox Code Playgroud) 假设我有这个 ViewModel 和 xaml:
class MyViewModel
{
public MyStringValue {get;set;} = "HelloWorld"
public IList<CustomObject> ChildViewModels{get;set;}
}
<DataTemplate DataType="{x:Type local:MyViewModel}">
<ItemsControl ItemsSource="{Binding ChildViewModels}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=MyStringValue,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type local:MyViewModel}}}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我不断收到此错误消息:“无法找到使用引用 'RelativeSource FindAncestor ... 进行绑定的源...” 所以基本上,我正在尝试绑定 ItemsControl 的父属性容器,但似乎我不能。
因此,我不时在 Winforms 中处理遗留应用程序,并且并不熟悉绑定对象的最佳实践。基本上,我有一个由三个部分组成的套装,其中有两个人,他们可能只有一个产品,但该产品可能会导致有不同的 SKU 集。有没有办法从第一个组合框的值触发组合框的事件和填充?我一直在环顾四周,我正在寻找有关如何绑定组合框的基本数据(我可以做到这一点),或者对如何绑定它做一些事情。触发依赖父项更改并更改数据集后不绑定。下面的例子:
POCOS:
Public Class Person
Public Property PersonID As Integer
Public Property FirstName As String
Public Property LastName As String
Public Property ProductId As Integer
Public Property SkuId As Integer
End Class
Public Class Product
Public Property ProductId As Integer
Public Property Description As String
End Class
Public Class Sku
Public Property SKUId As Integer
Public Property ProductId As Integer
Public Property Description As String
End Class
Run Code Online (Sandbox Code Playgroud)
主要代码示例(基本 UI 实际上只有一个标记为“ds”的数据集,该数据集与具有数据表的人员和产品 POCOS 几乎完全相同。数据网格视图“dgv”的列绑定到人员中的数据,除了名为 SKU 的列没有绑定因为我想在事后绑定它,这就是我失败的地方。
2016 年 …
vb.net data-binding datagridview datagridviewcombobox winforms
我对 C# 和 WPF 项目有点陌生。所以这是我的问题。
我有 2 个 Combobox 填充了字符串列表。
根据我的第一个组合框的值,我想更改第二个组合框中可用的列表。
这是我的代码:
public partial class MainWindow : Window
{
//creation de listes
List<string> themesE17 = new List<string>();
List<string> themesH17 = new List<string>();
List<string> themesE16 = new List<string>();
List<string> themesH16 = new List<string>();
public MainWindow()
{
InitializeComponent();
initLists();
string value = comboSaison.Text;
Console.WriteLine("The value of season combobox " + value);
}
public void initLists()
{
//saison 2017
themesE17.Add("Ete 17 Theme1");
themesE17.Add("Ete 17 Theme2");
themesH17.Add("Hiver 17 Theme1");
themesH17.Add("Hiver 17 Theme2");
//saison 2016 …Run Code Online (Sandbox Code Playgroud) 我一直在发疯,试图找到一个看似简单的问题的解决方案。我试图找到一种方法来遍历我的 ListView 并获取值。
我有一个带有绑定的 ListView,在 xaml 中看起来像这样(简化):
<ListView x:Name="MColumnsListXaml" HorizontalAlignment="Left" Height="Auto" Margin="0,42,0,0" VerticalAlignment="Top" Width="Auto">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" Width="Auto" DisplayMemberBinding="{Binding MColumnName}" />
<GridViewColumn Header="Last Name" Width="Auto" DisplayMemberBinding="{Binding MColumnName2}" />
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
我声明绑定:
public class MandatoryColumns
{
public string MColumnName { get; set; }
public string MColumnName2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
列由调用方法的事件触发器填充:
private void btnValidateColumns_Click(object sender, RoutedEventArgs e)
{
MandatoryOptionalColumnsList();
}
public void MandatoryOptionalColumnsList()
{
List<MandatoryColumns> MColumnsList = new List<MandatoryColumns>();
MColumnsList.Add(new MandatoryColumns() { MColumnName = "John", MColumnName2 = …Run Code Online (Sandbox Code Playgroud) 编辑
这不是字符串空值的回退的重复。我要求对复杂类型进行回退。
原始问题
这是我昨天问的一个问题的后续问题:
将 WPF 命令绑定到 C# XAML 中的 ViewModel 属性
我接受的答案的核心部分是这样的:
<Window.Resources>
<DataTemplate DataType="{x:Type local:SettingsPathSelectorViewModel}">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding SettingsPath}" />
<Button
Content="..."
Command="{Binding OpenFile}"
HorizontalAlignment="Left"
MinWidth="40"
Margin="4,0,0,0"
/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<Label>First Path</Label>
<ContentControl Content="{Binding FirstPath}" />
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
ADataTemplate是为自定义类型创建的,然后将 aContentControl绑定到该类型的属性。
现在的问题是,属性(FirstPath在示例中)可能是null并且没有呈现任何 UI 元素。我怎么能完成从DataTemplate属性呈现控件null
正如 Evk 建议的那样,我已经实现了一个转换器:
public class PathSelectorConverter : IValueConverter
{
public object Convert(object o, Type type, object parameter, CultureInfo culture) …Run Code Online (Sandbox Code Playgroud) 在数据绑定适配器中,我想检查int模型中的值是否不为零。因为从不显示提示,如果默认值为 0,则 0 显示为文本。如果值为零,我想显示提示。
下面无需检查 0 int 值即可正常工作
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/port"
android:inputType="number"
android:text="@={`` + item.port}"
/>
Run Code Online (Sandbox Code Playgroud)
我试过这个不起作用
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/port"
android:inputType="number"
android:text='@={item.port != 0 ? `` + item.port : ""}'
/>
Run Code Online (Sandbox Code Playgroud)
item.port是int价值
有什么建议可以只使用数据绑定来完成这项工作吗?
我最近一直在使用数据绑定,并且遇到了 executePendingBindings 方法。文档几乎没有显示,我可以理解它是如何工作的或何时使用它。这是该方法用法的示例。
请举例说明使用和不使用它的区别。谢谢
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Customer customer= List.get(position).second;
((CustomerViewHolder)holder).binding.setCustomer (customer)
((CustomerViewHolder)holder).binding.executePendingBindings();
}
Run Code Online (Sandbox Code Playgroud) 运行命令 ng serve 工作正常,因为 ng serve --prod 显示错误
ERROR in src\app\employee\employee.component.html(12,9): : Property 'emp' does not exist on type 'employee[]'.
src\app\employee\employee.component.html(23,9): : Property 'emp' does not exist on type 'employee[]'.
src\app\employee\employee.component.html(36,9): : Property 'emp' does not exist on type 'employee[]'.
Run Code Online (Sandbox Code Playgroud)
员工.component.html
<h2 mat-dialog-title>Edit details</h2>
<div mat-dialog-content>
<form
fxLayout="column"
[formGroup]="editForm"
#f="ngForm">
<div class="input-row">
<mat-form-field fxFlex>
<mat-label>Id</mat-label>
<input readonly
value="{{data.emp.id}}"
matInput #id disabled
formControlName="id"
required/>
</mat-form-field>
</div>
<div class="input-row">
<mat-form-field fxFlex>
<mat-label>Name</mat-label>
<input
value="{{data.emp.name}}"
matInput #name
placeholder="Email"
formControlName="name"
required/>
</mat-form-field>
</div>
<div …Run Code Online (Sandbox Code Playgroud) 我为加载图像 url 编写了一个自定义属性,如下所示:
@BindingAdapter("srcCircleUrl")
fun loadCircleImage(view: ImageView, imageUrl: String) {
loadImage(view.context, imageUrl, view, options = circleCropTransform())
}
Run Code Online (Sandbox Code Playgroud)
当我想在 xml 中设置一个原始字符串时,它给了我srcCircleUrl attribute not found错误。
例如,如果我写这样的东西,它不起作用:
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:srcCircleUrl="https://66.media.tumblr.com/97bcd9782631f8bef87bb30e830344a6/tumblr_owxu10zbPB1tl4ciuo4_250.png"
android:scaleType="centerCrop"
tools:srcCompat="@drawable/flag_iran" />
Run Code Online (Sandbox Code Playgroud)
所以,问题是,如何将原始字符串作为自定义数据绑定属性的输入?
我还测试了这些方法:
app:srcCircleUrl="@{https://66.media.tumblr.com/97bcd9782631f8bef87bb30e830344a6/tumblr_owxu10zbPB1tl4ciuo4_250.png}"
app:srcCircleUrl="@{`https://66.media.tumblr.com/97bcd9782631f8bef87bb30e830344a6/tumblr_owxu10zbPB1tl4ciuo4_250.png`}"
Run Code Online (Sandbox Code Playgroud) data-binding ×10
c# ×5
wpf ×4
android ×3
combobox ×2
winforms ×2
xaml ×2
angular ×1
angular6 ×1
datagridview ×1
datatemplate ×1
listview ×1
loadimage ×1
mvvm ×1
vb.net ×1