gpt4 book ai didi

wpf - ItemsControl 中的绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-12-03 10:26:29 25 4
gpt4 key购买 nike

我有一堆不同的控件(主要是里面有文本 block 的按钮),我只是把它们放在一个 ItemsControl 中。在我将控件放入 ItemsControl 之前,所有绑定(bind)都正常工作。现在,没有任何命令起作用,也没有文本绑定(bind)。一切都显示为 0(我绑定(bind)到 double )。我仔细检查以确保我的 ObservableCollection实际上充满了项目,并且这些项目的属性实际上包含数据。 ItemsControl 确实为集合中的每个项目正确地创建了一个新行。

这是我的模型:

public class VehicleModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private List<double> _nowTime = new List<double>();
public List<double> NowTime
{
get { return _nowTime; }
set { _nowTime = value; OnPropertyChanged("Nowtime"); }
}

private List<double> _VehLat = new List<double>();
public List<double> VehLat
{
get { return _VehLat; }
set { _VehLat = value; OnPropertyChanged("VehLat"); }
}

private int _currentIteration;
public int CurrentIteration //used to hold current index of the list of data fields
{
get { return _currentIteration; }
set
{
_currentIteration = value;
OnPropertyChanged("CurrentIteration");
OnPropertyChanged("CurrentVehLat");
}
}

private double _currentVehLat;
public double CurrentVehLat
{
get { return _currentVehLat; }
set { _currentVehLat = VehLat[CurrentIteration]; OnPropertyChanged("CurrentVehLat"); }
}
}

//Used to loop through the above list and set the currentVehLat equal to
//the current iteration of the list
public void SetData(int i)
{
CurrentIteration = i;
}

在我的 View 模型中,我有一个 ObservableCollection拿着这些 VehicleModel年代:
 private ObservableCollection<VehicleModel> _vehicleCollection = new ObservableCollection<VehicleModel>();
public ObservableCollection<VehicleModel> VehicleCollection
{
get
{
return _vehicleCollection;
}
set
{
if (null != value)
{
_vehicleCollection = value;
OnPropertyChanged("VehicleCollection");
}
}
}

private ICommand showTimeWindowCmd;
public ICommand ShowTimeWindowCmd
{
get
{
return showTimeWindowCmd;
}
set
{
showTimeWindowCmd = value;
}
}

public MainWindowViewModel()
{
ShowTimeWindowCmd = new RelayCommand(ShowTimeWindow, param => this.canExecute);
}

public void ShowTimeWindow(object parameter)
{
//do stuff
}

最后,我的 ItemsControl 的 .xaml。我只显示一个,因为它们很多,但它们都完全相同,只是绑定(bind)到不同的属性(所有 double 都像 View 模型中显示的那样)。注意:控件正确显示,而不是绑定(bind):
<ItemsControl Grid.Row="8"
Grid.ColumnSpan="16"
ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
//bunch here
</Grid.ColumnDefinitions>
<Button Grid.ColumnSpan="4"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Command="{Binding ShowTimeWindowCmd}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="NowTime" />
<Binding Path="VehLat" />
<Binding Source="FISH Latitude" />
<Binding />
</MultiBinding>
</Button.CommandParameter>
<Button.Template>
<ControlTemplate>
<TextBlock FontSize="17"
Text="{Binding Path=CurrentVehLat,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
StringFormat={}{0:F7}}"
Visibility="{Binding IsChecked,
ElementName=FishChkBox,
Converter={StaticResource BoolToVisConverter}}" />
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

编辑:我正在设置我的数据上下文:
<Window.DataContext>
<viewmodel:MainWindowViewModel />
</Window.DataContext>

编辑 2:添加了在 View 模型中调用的命令。前两个命令参数是模型的属性。

最佳答案

由于 ItemsSource 的 DataContext 是 Model 的集合,对于内部控件,这也将是它的 DataContext,因此您需要显式指定 Path 以指向 ViewModel 属性:

<Button Grid.ColumnSpan="4"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}},
Path=DataContext.ShowTimeWindowCmd}"
>

关于wpf - ItemsControl 中的绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41041261/

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