C# 绑定XAML中Itemscontrol之外的属性
我正在尝试绑定Itemscontrol之外的属性。 然而,这似乎不起作用 似乎在ItemsControl、DataTemplate中,它指的是集合内部的内容,而不是集合外部的内容。 我尝试了RelativeResource,并参考了ViewModel的AncestorType 代码(VM):C# 绑定XAML中Itemscontrol之外的属性,c#,.net,wpf,xaml,caliburn.micro,C#,.net,Wpf,Xaml,Caliburn.micro,我正在尝试绑定Itemscontrol之外的属性。 然而,这似乎不起作用 似乎在ItemsControl、DataTemplate中,它指的是集合内部的内容,而不是集合外部的内容。 我尝试了RelativeResource,并参考了ViewModel的AncestorType 代码(VM): 公共类测试{ 公共字符串GetThis{get{return“123”}set{} 公共列表迭代器属性{get;set;} } XAML(视图): 您需要绑定到父级ItemsControl的DataCon
公共类测试{
公共字符串GetThis{get{return“123”}set{}
公共列表迭代器属性{get;set;}
}
XAML(视图):
您需要绑定到父级
ItemsControl
的DataContext
<ItemsControl ItemsSource="{Binding Path=IterateProperty}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.GetThis,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ItemsControl}}}" />
我在这方面做了一个快速完整的示例:
<Window x:Class="ParentDataContext.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
<TextBlock Margin="5"
Text="{Binding Path=DataContext.TextFromParent,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
以下是代码隐藏:
public partial class MainWindow : Window
{
public string TextFromParent
{
get { return (string)GetValue(TextFromParentProperty); }
set { SetValue(TextFromParentProperty, value); }
}
// Using a DependencyProperty as the backing store for TextFromParent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextFromParentProperty =
DependencyProperty.Register("TextFromParent", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));
public ObservableCollection<Model> items { get; set; }
public MainWindow()
{
InitializeComponent();
items = new ObservableCollection<Model>();
items.Add(new Model() { IsChecked = true });
items.Add(new Model() { IsChecked = false });
items.Add(new Model() { IsChecked = true });
items.Add(new Model() { IsChecked = false });
TextFromParent = "test";
this.DataContext = this;
}
}
因此,您可以访问在父对象的DataContext上定义的属性
嗯,精确定位很好,但我还是有一个错误。但它现在显示:无法在类型为“System.Windows.Controls.ItemsControl”noooooo的数据上下文中解析属性“…”(似乎我仍然遇到同样的问题。我使用的是caliburns MVVM。(抱歉,也忘了提及这一点)如果
GetThis
和IterateProperty
真的在同一个类和公共中……那么Mike的代码肯定能用。@Khiem KimHoXuan你确定你没有忘记一些重要的代码吗?似乎现在能用了。我不得不再次检查我的代码。Datacontext.Propertyname和UserControl解决了我的问题problem@Clemens哦,好的l、 这不是编辑的重要部分…我编辑只是为了向代码中添加上下文。这可能是一个小的添加,但是那些正在与相对资源绑定作斗争的人应该以这种方式更清楚地看到它。你能检查一下我关于如何定义属性的示例吗?这可能会有所帮助。谢谢兄弟。这很有帮助
<Window x:Class="ParentDataContext.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
<TextBlock Margin="5"
Text="{Binding Path=DataContext.TextFromParent,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
Text="{Binding Path=DataContext.TextFromParent,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
public partial class MainWindow : Window
{
public string TextFromParent
{
get { return (string)GetValue(TextFromParentProperty); }
set { SetValue(TextFromParentProperty, value); }
}
// Using a DependencyProperty as the backing store for TextFromParent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextFromParentProperty =
DependencyProperty.Register("TextFromParent", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));
public ObservableCollection<Model> items { get; set; }
public MainWindow()
{
InitializeComponent();
items = new ObservableCollection<Model>();
items.Add(new Model() { IsChecked = true });
items.Add(new Model() { IsChecked = false });
items.Add(new Model() { IsChecked = true });
items.Add(new Model() { IsChecked = false });
TextFromParent = "test";
this.DataContext = this;
}
}
public class Model : INotifyPropertyChanged
{
private bool _IsChecked;
public bool IsChecked
{
get { return _IsChecked; }
set
{
_IsChecked = value;
PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}