gpt4 book ai didi

c# - DataTemplate 中的 WPF ItemsSource 绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:44 25 4
gpt4 key购买 nike

<分区>

我创建了一个用于在我的应用程序中选择状态的自定义控件,称为 StateSelector。如果我将它放在我的 UserControl 中并绑定(bind) ItemsSource(通过一个名为 StateList 的自定义属性),它就可以正常工作。但是在 DataTemplate 中使用相同的绑定(bind)不起作用。

这是我的 StateSelector.xaml

<UserControl x:Class="MyNamespace.StateSelector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:MyNamespace"
Height="21" Width="60">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="0"
Name="cboState"
SelectedItem="{Binding Path=CurrentState, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
ItemsSource="{Binding Path=StateList, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Code}" />
<TextBlock Text="{Binding Path=Name}" Margin="5,0,0,0" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</UserControl>

这是我的 StateSelector.xaml.cs

public class StateSelector : UserControl
{
public ObservableCollection<State> StateList
{
get { return (ObservableCollection<State>)GetValue(StateListProperty); }
set { SetValue(StateListProperty, value); }
}
public static readonly DependencyProperty StateListProperty =
DependencyProperty.Register("StateList", typeof(ObservableCollection<State>), typeof(StateSelector));

public State CurrentState
{
get{ return (State)GetValue(CurrentStateProperty); }
set{ SetValue(CurrentStateProperty, value); }
}
public static DependencyProperty CurrentStateProperty =
DependencyProperty.Register("CurrentState", typeof(State), typeof(StateSelector));

/// <summary>
/// Default constructor for StateSelector
/// </summary>
public StateSelector()
{
InitializeComponent();
}
}

然后在包含 UserControl 的构造函数中,我用 State 对象填充 ObservableCollection。这是 MyDisplay.xaml.cs

public class MyDisplay : UserControl
{
private static DependencyProperty StateListProperty =
DependencyProperty.Register("StateList", typeof(ObservableCollection<State>), typeof(RateTableDisplay));
public ObservableCollection<State> StateList
{
get{ return (ObservableCollection<State>)GetValue(StateListProperty); }
set{ SetValue(StateListProperty, value); }
}

// Code to define SourceObject as seen in following XAML omitted

public MyDisplay()
{
InitializeComponent();

StateManager stateManager = new StateManager();
StateList = new ObservableCollection<State>(stateManager.GetAll(Context));
}
}

在 MyDisplay 的 XAML 中,我有一个部分按原样使用 StateSelector,另一个部分在 DataTemplate 中使用它。这是 MyDisplay.xaml

<UserControl x:Class="MyNamespace.MyDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Local="clr-namespace:MyNamespace"
Height="Auto" Width="Auto"
Name="_this"
DataContext="{Binding ElementName=_this, Path=SourceObject}">

<UserControl.Resources>
<ResourceDictionary>
<DataTemplate x:Key="OriginStateCellTemplate">
<Local:StateSelector StateList="{Binding ElementName=_this, Path=StateList}" />
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>

<!-- Unrelated XAML omitted -->
<!-- The StateSelector below gets populated properly -->
<StackPanel>
<Local:StateSelector Grid.Column="3" StateList="{Binding ElementName=_this, Path=StateList}" x:Name="cmbMasterOriginState" />
</StackPanel>

<!-- More Unrelated XAML omitted -->
<!-- The StateSelector below does not get populated at all -->
<StackPanel>
<ListView Height="610">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn x:Name="origStateCol" Width="85" CellTemplate="{StaticResource OriginStateCellTemplate}">
<GridViewColumnHeader Click="header_Click" Tag="OriginState">Origin State</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</UserControl>

我尝试了不同的方式来绑定(bind) StateList,例如:

StateList="{Binding Path=StateList, Source={x:Reference _this}}"
StateList="{Binding Path=StateList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"

但没有这样的运气。我不明白为什么在 DataTemplate 示例中没有填充 ItemsSource。它在 DataTemplate 之外工作正常。我在 Visual Studio 的调试输出中也没有收到任何绑定(bind)错误。

在搜索时,我发现了很多相关问题,但它们似乎都在尝试使用在顶部 UserControl 元素中定义的通用 DataContext。我专门尝试使用 UserControl 本身的属性。

有什么想法吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com