找到了这么多示例,但没有一个符合!我的列表框是一个结果对象列表。可以在列表框中选中或取消选中结果,以将其标记为“允许”传输。
<ListBox
x:Name="FileListBox"
ItemsSource="{Binding TestResults}"
ItemTemplate="{StaticResource FileListTemplate}"
SelectionMode="Single"
SelectedItem="{Binding FileListSelected}"
Background="#FFFFFBE2" />The FileListTemplate
<DataTemplate x:Key="FileListTemplate">
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".5*" />
<ColumnDefinition Width=".3*" />
<ColumnDefinition Width=".2*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding FileName}" />
<TextBlock Grid.Column="1"
Text="Machine">
</TextBlock>
<CheckBox x:Name="UploadOK"
Grid.Column="2"
HorizontalAlignment="Right"
IsChecked="{Binding CanUpload, Mode=TwoWay}" />
</Grid>
</DataTemplate>我去掉了很多格式化代码以减少混乱。因此,当复选框被选中(或取消选中)时,我需要将对象上的布尔值设置为true或false。但是我不希望仅仅因为复选框被选中就选择ListItem。当选择ListItem时,会发生其他事情。下面是实现这一点的代码。
public TestResult FileListSelected
{
get
{
return selectedItem;
}
set
{
if (value == selectedItem)
return;
selectedItem = value;
if (!Workspaces.Any(p => p.DisplayName == value.FileName))
{
this.DisplayTestResult(value as TestResult);
}
base.RaisePropertyChanged("FileListSelected");
}
}下面是我为Checkbox绑定的代码(尽管它不起作用)。
public bool CanUpload
{
get { return selectedItem.CanUpload; }
set
{
selectedItem.CanUpload = value;
}
}我很感谢你看这个。
发布于 2017-01-11 22:06:02
使用MVVM时,请始终检查以下各项:
从INotifyPropertyChanged
using System.ComponentModel;检查DataContext并查看Output窗口中的BindingErrors
示例属性:
public string Example
{
get { return _example; }
set
{
_example= value;
OnPropertyChanged();
}
}每当一个新值被赋值为时,它将自动调用OnPropertyChanged (当它从其他位置改变时,不会自动更新!)
确保您的INotifyPropertyChanged实现如下所示:
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}为此,您还需要using System.Runtime.CompilerServices;
让您的代码正常工作的其他选项:
应为ObservableCollection<TestResult>
TestResults应具有CanUpload和FileName的属性并从INotifyPropertyChanged继承
然后在你的MainViewModel上,例如on和ButtonClick,你可以像这样获得选定的文件:
private List<string> GetSelectedFiles()
{
return TestResults.Where(result => result.CanUpload == true).Select(r => r.FileName).ToList());
}备注:
FileListSelected是ListBox的DataContext的一个属性,它与条目的DataContext不同(至少应该是这样)。
然后,FileListSelected将返回您的ItemsSource的选定项目。
也许你可以通过行选择/复选框来评论这个问题,并添加一些细节,这样我就可以为你提供更多帮助。
编辑:通知MainWindowViewModel CheckBox状态更改:
我认为这里有两种可能的方法:
使用EVENT的
将以下代码添加到您的TestResult类:
public delegate void CheckBoxStateChangedHandler(object sender, CheckBoxStateChangedEventArgs e);
public event CheckBoxStateChangedHandler CheckBoxStateChanged;
public class CheckBoxStateChangedEventArgs
{
bool CheckBoxChecked { get; set; }
}确保在您的MainViewModel中创建新的TestResult时,您订阅了该event;
testResult.CheckBoxStateChanged += CheckBox_StateChanged;在CheckBox_StateChanged中更改状态后处理您想要执行的操作。请注意,参数e包含布尔值(选中)和作为发送方的相应TestResult。
您只需在CheckBox.Checked Binding的Setter中调用新事件
public bool Checked
{
get { return _checked; }
set
{
_checked = value;
OnPropertyChanged();
CheckBoxStateChanged.Invoke(this, new CheckBoxStateChangedEventArgs() { CheckBoxChecked = value })
}
}MAINWINDOWVIEWMODEL上的
调用方法
为此,您需要创建MainWindowViewModel的static对象(在MainViewModel中)-在创建MainWindowViewModel后,不要忘记赋值。
public static MainViewModel Instance { get; set; }然后,只需根据需要添加一个公共方法:
public void CheckBoxValueChanged(bool value, TestResult result)
{
//Do whatever
}您也可以从上面的事件被调用的同一地点调用。
public bool Checked
{
get { return _checked; }
set
{
_checked = value;
OnPropertyChanged();
MainWindowViewModel.Instance.CheckBoxValueChanged(value, this);
}
}发布于 2017-01-11 22:30:08
Internal Class TestResult
{
...
private bool _canUpload;
public bool CanUpload
{
get { return _canUpload; }
set
{
_canUpload = value;
base.RaisePropertyChanged("CanUpload");
}
}
}https://stackoverflow.com/questions/41592474
复制相似问题