对于XAML和MVVM,我正在为我们的项目构建维护屏幕。组合框从从数据库检索的列表中填充。当从组合框中选择一个值时,我想从所选项目中读取其中一个属性,并检查2个单选按钮中的1个,然后使用相同的所选项目填充列表框。这是我的xaml:
<UserControl x:Class="StatementPrinting.Client.Views.Setup.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:telerikRibbonView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.RibbonView"
xmlns:examples="clr-namespace:Telerik.Windows.Controls.RichTextBoxUI"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<telerik:RadBusyIndicator IsBusy="{Binding BusyDetail.IsBusy}" BusyContent="{Binding}"
IsIndeterminate="True"
BusyContentTemplate="{StaticResource BusyIndicatorBusyContentTemplate}">
<!--<Grid MinWidth="650" MinHeight="400">-->
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0" Margin="5,0,5,20">
<TextBlock FontWeight="Bold" FontSize="18">Report Group Setup</TextBlock>
<TextBlock>Here you edit whether the selected group is printed or not and what items are part it.</TextBlock>
</StackPanel>
<Grid Grid.Column="0" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0" Grid.Row="0">
<TextBlock VerticalAlignment="Center" Margin="0,0,15,0">Select a Group:</TextBlock>
<ComboBox x:Name="cmbReportGroup" IsEditable="False" ItemsSource="{Binding ReportGroups}" DisplayMemberPath="Description" SelectedValuePath="Name" SelectedItem="{Binding SelectedReportGroup}" />
</DockPanel>
<DockPanel Grid.Column="1" Grid.Row="0" Margin="40,0,0,0">
<TextBlock VerticalAlignment="Center">Printing?</TextBlock>
<RadioButton x:Name="printYesRadioButton" Margin="10" GroupName="printRadioButtonGroup" Content="Yes">
<RadioButton.Style>
<Style TargetType="{x:Type RadioButton}">
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cmbReportGroup, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
<TextBlock Name="Or_TextBlock" VerticalAlignment="Center" Margin="5,10,5,10">or</TextBlock>
<RadioButton x:Name="printNoRadioButton" Margin="10" GroupName="printRadioButtonGroup" Content="No">
<RadioButton.Style>
<Style TargetType="{x:Type RadioButton}">
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cmbReportGroup, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
</DockPanel>
</Grid>
<Grid Grid.Column="0" Grid.Row="2" Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" Margin="0,0,0,10">
<TextBlock>
Change the items that belong to this group:
</TextBlock>
</StackPanel>
<DockPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2">
<TextBlock DockPanel.Dock="Top" Margin="0,0,0,5">Current Notices:</TextBlock>
</DockPanel>
<DockPanel Grid.Column="2" Grid.Row="1">
<TextBlock DockPanel.Dock="Top" Margin="0,0,0,5">Available Notices:</TextBlock>
</DockPanel>
<DockPanel Grid.Column="0" Grid.Row="2">
<ListBox></ListBox>
</DockPanel>
<DockPanel Grid.Column="1" Grid.Row="2">
<Button DockPanel.Dock="Top" VerticalAlignment="Center" Margin="10">
<Image Source="c:\_MicrosProjects\StatementPrinting\DEV\StatementPrinting\StatementPrinting.Client\Resources\arrow-left-10.png"/>
</Button>
<Button DockPanel.Dock="Top" VerticalAlignment="Center" Margin="10">
<Image Source="c:\_MicrosProjects\StatementPrinting\DEV\StatementPrinting\StatementPrinting.Client\Resources\arrow-right-10.png"/>
</Button>
</DockPanel>
<DockPanel Grid.Column="2" Grid.Row="2">
<ListBox></ListBox>
</DockPanel>
</Grid>
</Grid>
</telerik:RadBusyIndicator>
代码隐藏:
using Mbs.Common.Instrumentation;
using Mbs.Mvvm.Core;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using StatementPrinting.Client.ViewModels.Base;
using StatementPrinting.DataLayer;
using StatementPrinting.Domain;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StatementPrinting.Client.ViewModels.Setup
{
public class TestViewModel : SecurityTabbedWorkspaceViewModelBase
{
public TestViewModel(IScreenViewModel screenVM, string connectionString, IEventAggregator eventAggregator)
{
this.SetNameAndTitle("Report Group Setup");
this.ScreenVM = screenVM;
this.ConnectionString = connectionString;
this.AppEventAggregator = eventAggregator;
this.SetSecurity();
LoadReportGroups();
}
private readonly ObservableCollection<ReportGroup> _ReportGroups = new ObservableCollection<ReportGroup>();
public ObservableCollection<ReportGroup> ReportGroups
{
get { return _ReportGroups; }
}
private string _SelectedIndex;
public string SelectedIndex { get; set; }
private ReportGroup _SelectedReportGroup;
public ReportGroup SelectedReportGroup
{
get { return _SelectedReportGroup; }
set
{
_SelectedReportGroup = value;
MessageBox.Show("Print: " + SelectedReportGroup.Print.ToString());
SetPrintOption(SelectedReportGroup.Print);
this.RaisePropertyChanged("SelectedReportGroup");
}
}
private void SetPrintOption(bool PrintOption)
{
MessageBox.Show("Print 2: " + SelectedReportGroup.Print.ToString());
if (SelectedReportGroup.Print)
{
//printYesRadioButton.IsChecked = true;
}
else
{
//printNoRadioButton.IsChecked = true;
}
}
private void LoadReportGroups()
{
try
{
CancellationToken cancelToken = this.GetCancellationToken();
this.BusyDetail.CanBeCanceled = true;
this.BusyDetail.TurnOnBusyIndicator("Loading...");
Task<List<ReportGroup>> loadTask = Task.Factory.StartNew<List<ReportGroup>>(() =>
{
using (StatementPrintingContext context = new StatementPrintingContext(ContextConnectionStringHelper.GetEntitiesConnectString(this.ConnectionString)))
{
return context.ReportGroups.ToList();
}
},
cancelToken);
var completedTask = loadTask.ContinueWith((t) =>
{
if (t.Exception != null)
{
this.RaiseErrorMessageNotification(t.Exception.ToString());
}
else
{
t.Result.ForEach(r => this.ReportGroups.Add(r));
//
// or
//
//foreach (var r in t.Result.ToList())
//{
// ReportGroups.Add(r);
//}
}
},
cancelToken, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
catch (Exception ex)
{
this.RaiseErrorMessageNotification(ex.ToString());
}
finally
{
this.BusyDetail.TurnOffBusyIndicator();
}
}
private DelegateCommand _SaveChangesCommand;
public DelegateCommand SaveChangesCommand
{
get
{
if (_SaveChangesCommand == null)
{
_SaveChangesCommand = new DelegateCommand(() =>
{
try
{
using (var db = new StatementPrintingContext(ContextConnectionStringHelper.GetEntitiesConnectString(this.ConnectionString)))
{
// ******Do any database changes here!********
// . . .
// . . .
this.SaveChangesCommand.RaiseCanExecuteChanged();
this.RaiseMessageNotification("Changes saved successfully.");
}
}
catch (Exception ex)
{
this.RaiseErrorMessageNotification(ex.Message);
ExceptionLogger.Instance.WriteExceptionTrace(ex, this.GetType().ToString());
}
},
() =>
{
// *******Put conditional code here!***********
// . . .
// . . .
return true;
});
}
return _SaveChangesCommand;
}
}
private DelegateCommand _TestCommand;
public DelegateCommand TestCommand
{
get
{
if (_TestCommand == null)
{
_TestCommand = new DelegateCommand(() =>
{
try
{
this.TestCommand.RaiseCanExecuteChanged();
}
catch (Exception ex)
{
this.RaiseErrorMessageNotification(ex.Message);
ExceptionLogger.Instance.WriteExceptionTrace(ex, this.GetType().ToString());
}
},
() =>
{
return true;
});
}
return _TestCommand;
}
}
}
}
重申一下,一个人从组合框中选择一个项目。当他们这样做时,它会读取存储在数据库中的该项目的打印值,然后检查“是”或“否”单选按钮。这是我现在遇到的主要问题。谢谢你的帮助。
答案 0 :(得分:0)
在ViewModel中添加一个名为SelectedName
的属性,并将其绑定到ComboBox的SelectedValue:
<ComboBox x:Name="cmbReportGroup" IsEditable="False" ItemsSource="{Binding ReportGroups}" DisplayMemberPath="Description" SelectedValuePath="Name" SelectedItem="{Binding SelectedReportGroup}" SelectedValue="{Binding SelectedName}" />
并在SelectedName属性的Setter中的ViewModel中添加您的代码:
public string SelectedName
{
get { return _SelectedName; }
set
{
_SelectedName= value;
this.RaisePropertyChanged("SelectedName");
// do your data retrieving form database ...
CheckPrintYesOrNow();
}
}
对于RadioButton
,您可以使用相同的方式在ViewModel中添加bool
属性并绑定到。