我正在使用MVVM模式构建我的第一个WPF.在这个社区的帮助下,我设法创建了我的模型,我的第一个ViewModel和视图.现在我想为设计基本应用程序布局界面的应用程序添加一些复杂性.我的想法是至少有2个子视图和一个主视图,并将它们分成几个XAML:
- Main.XAML
- Products.XAML
- Clients.XAML
Main将有一个菜单和一个空间来加载子视图(产品和客户端).现在遵循MVVM模式,视图之间的所有导航逻辑都应该在ViewModel上写入.所以mi想法是拥有4个ViewModel:
- MainViewModel
- ProductsViewModel
- ClientsViewModel
- NavigationViewModel
那么NavigationViewModel应该包含一个子视图模型的集合?一个活跃的viewmodel是吗?
所以我的问题是:
1)如何使用MVVM模式在主视图上加载不同的视图(产品,客户端)?
2)如何实现导航viewModel?
3)如何控制打开或活动视图的最大数量?
4)如何在打开的视图之间切换?
我一直在做大量的搜索和阅读,并且找不到任何简单的MVVM导航示例,其中WPF在主视图中加载了多个视图.许多人:
1)使用外部工具包,我现在不想使用它.
2)将所有视图的所有代码放在一个XAML文件中,这似乎不是一个好主意,因为我需要实现近80个视图!
我在这里正确的道路?任何帮助,特别是一些代码将不胜感激.
UPDATE
所以,我按照@LordTakkera的建议建立了一个测试项目,但是卡住了.这就是我的解决方案的样子:

我创造:
两种型号(客户和产品)
一个MainWindow和两个wpf用户控件(客户端和产品)XAML.
三个ViewModel(客户端,产品和主ViewModel)
然后我将每个视图上的dataContext设置为相应的viewModel.之后,我使用ContentPresenter创建MainWindow,并将其绑定到viewmodel的属性.
MainWindow.XAML
<Window x:Class="PruevaMVVMNavNew.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="519" Width="890">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Border Grid.Column="0" Grid.ColumnSpan="2" Background="AntiqueWhite" ></Border>
<Border Grid.Row="1" Grid.RowSpan="2" Background="AliceBlue"></Border>
<Border Grid.Row="1" Grid.Column="1" Background="CadetBlue"></Border>
<ContentPresenter Grid.Row="1" Grid.Column="1" x:Name="ContentArea" Content="{Binding CurrentView}"/>
<StackPanel Margin="5" Grid.Column="0" Grid.Row="1">
<Button>Clients</Button>
<Button>Products</Button>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
这也是来自MainWindow的viewmodel:
class Main_ViewModel : …Run Code Online (Sandbox Code Playgroud) 我需要一些帮助!我正在尝试使用Entity Framework,WCF,MVVM和WPF技术创建我的第一个应用程序.我是所有人的新手.所以我使用Entity Framework为我的数据库创建了一个模型.然后我创建我的WCF服务类.我已经阅读了近50篇关于EF和WCF服务的文章,我现在都在混淆.我知道我不应该直接暴露我的模型.目前我正在使用此代码作为服务合同:
namespace Ohmio.DataService
{
[ServiceContract]
public class OhmioService
{
[OperationContract]
public IEnumerable<vw_Pedidos> ListarPedidos(string IDComprobante, bool bEntregados, int Numero = -1, int IDCliente = -1)
{
using (var context = new OhmioTestNet())
{
string sqlString="SELECT VALUE cs FROM OhmioTestNet.vw_Pedidos AS cs WHERE cs.ID_Comprobante=='" + IDComprobante + "' AND ";
if (Numero != -1) sqlString += "cs.Numero=="+ Numero.ToString() +" AND ";
if (IDCliente != -1) sqlString += "cs.ID_Cliente=="+ IDCliente.ToString()+" AND ";
if (!bEntregados) sqlString += "cs.Entregado==false AND ";
sqlString =sqlString.Substring(0,sqlString.Length-4); …Run Code Online (Sandbox Code Playgroud) 我是WPF的新手,我想为我的应用程序创建一个横向菜单.搜索我发现这张图片的想法:

我的想法是在图片中添加一个相反的按钮.当用户单击按钮时,它会展开按钮以显示子菜单选项.一次只能扩展一个菜单.我的第一个测试是使用列表框,内部使用每个按钮的扩展器,然后使用堆栈面板添加子菜单选项.它看起来像这样:

这是我的XAML:
<Window x:Class="InterfazOhmio.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" Background="Gray">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox>
<ListBox.Resources>
<Style TargetType="{x:Type Expander}">
<Setter Property="IsExpanded"
Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
</Style>
</ListBox.Resources>
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<ItemsPresenter/>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<Expander Background="GreenYellow" Width="243" Header="Pedidos">
<StackPanel>
<RadioButton Margin="20,5,5,5" Content="Nuevo Pedido" GroupName="Two"/>
<RadioButton Margin="20,5,5,5" Content="Consultar Pedidos" GroupName="Two"/>
<RadioButton Margin="20,5,5,5" Content="Pedidos Pendientes" GroupName="Two"/>
</StackPanel>
</Expander>
<Expander Background="BurlyWood" Width="243" …Run Code Online (Sandbox Code Playgroud) 我是LINQ的新手.我正在使用这个功能:
public IEnumerable<Vendedores> GetVendedores()
{
using (var context = new OhmioEntities())
{
Vendedores _allvendors= new Vendedores();
_allvendors.Nombre = "All Vendors";
_allvendors.ID_Vendedor = -1;
var query = context.Vendedores;
var _vendors= query.Where(f => f.Activo == true).OrderBy(o=>Nombre).ToList();
_vendors.Insert(0, _allvendors);
return _vendors;
}
}
Run Code Online (Sandbox Code Playgroud)
它应该给我活跃供应商的订单清单.where部分工作正常,但忽略顺序,.ToList之后的记录是原始表顺序.我做错了什么?谢谢!
我有一个主键字段ID的表.我不想使用Identity,因为我需要为用户提供手动选择新对象的ID的可能性.所以我的想法是:
问题是如何查询和SQL Server表获取第一个免费ID号?
例1:
ID
--
1
2
10
Run Code Online (Sandbox Code Playgroud)
首个免费ID为3
例2:
ID
--
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
首个免费ID为5
有没有办法做到这一点?我能想到的是获取最小值和最大值,为可能的值创建一个循环,然后与表数据进行比较,但它涉及到数据库的太多查询.谢谢!
我正在从 .NET Core 2 o 3 版本迁移现有的 Web API。经过几个问题,我设法使它工作,除了按列名的 Dynamic OrderBy。
这是我的代码,与 .net core 2 配合使用效果很好:
public async Task<IEnumerable<Clientes_view>> GetClientes(int bActivos, int nRegistroInic, int nRegistros, string sOrdenar,
int nSentido, string sFiltro, int nTipo = -1, int idCliente = -1)
{
var clientes = this.context.Set<Clientes_view>()
.Where(e => e.RazonFantasia.Contains(sFiltro) || e.RazonFantasia.Contains(sFiltro)
|| e.Cuit.Contains(sFiltro) || e.Mail.StartsWith(sFiltro) || string.IsNullOrEmpty(sFiltro))
.Where(e => (e.Activo && bActivos == 1) || bActivos == -1 || (!e.Activo && bActivos == 0))
.Where(e => e.IdTipoCliente == nTipo || nTipo == …Run Code Online (Sandbox Code Playgroud) 我只是好奇.如果我有两张桌子,那就说客户和订单.客户端具有唯一的主键ID_Client.订单还有一个ID_Client字段,以及通过ID_Client字段维护客户表的完整性的关系.
所以,当我想加入两个表时,我会:
SELECT
Orders.*, Clients.Name
FROM
Orders
INNER JOIN
Clients ON Clients.ID_Client = Orders.ID_Client
Run Code Online (Sandbox Code Playgroud)
因此,如果我接受了创建主键的工作,以及表之间的关系,
我有必要在on子句中明确包含连接列吗?
为什么我不能这样做:
SELECT
Orders.*, Clients.Name
FROM
Orders
INNER JOIN
Clients
Run Code Online (Sandbox Code Playgroud)
所以SQL应该知道哪些列与两个表相关...
我总是问自己,为什么如果在SQL Server上有两个表,我创建一个这样的视图:
CREATE VIEW vw_MyView
AS
SELECT T1.*, T2.ClientName FROM Table1 T1 inner join Table2 T2 on T1.ID_Client=T2.ID_Client
Run Code Online (Sandbox Code Playgroud)
当我需要向表1添加字段时,视图显示值未命中?第一列的值显示在colunm 2上,依此类推!
这发生在我测试的每个版本的SQL Server上.任何人都可以告诉我为什么会发生这种情况以及如何解决这个问题?
这种行为给出了很大的问题,特别是对于派生的视图.谢谢
我正在建立一个WPF UserControl.为此,我实现了ItemSource DependecyProperty这样的:
private IEnumerable MisItems;
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(TextBoxAutoComplete), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = sender as TextBoxAutoComplete;
if (control != null)
control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
}
private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
MisItems = newValue;
// Remove handler for oldValue.CollectionChanged
var oldValueINotifyCollectionChanged = oldValue as INotifyCollectionChanged;
if (null …Run Code Online (Sandbox Code Playgroud) 是否有更有效的方式编写遵循XAML代码?(当我说效率更高时,我意味着更少的重复性).特别是超链接和扩展器.谢谢!
<Window x:Class="InterfazOhmio.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">
<Window.Background>
<ImageBrush ImageSource="Imagenes/background.jpg"></ImageBrush>
</Window.Background>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.Resources>
<Style TargetType="{x:Type Expander}">
<Setter Property="IsExpanded"
Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
</Style>
</ListBox.Resources>
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<ItemsPresenter/>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<Expander Margin="2" Background="OliveDrab">
<Expander.Header>
<BulletDecorator>
<BulletDecorator.Bullet>
<Image Source="Iconos/Pedidos.png" Width="64" Height="64" HorizontalAlignment="Left" VerticalAlignment="Top" />
</BulletDecorator.Bullet>
<TextBlock Margin="10,0,0,0" Text="Pedidos" VerticalAlignment="Center" HorizontalAlignment="Stretch" Foreground="White" />
</BulletDecorator>
</Expander.Header> …Run Code Online (Sandbox Code Playgroud) 我的问题很简单.
每次员工在时钟上使用时间卡时,我都会使用此表保存一行:
EmployeeID DateTime
---------- --------------------
1 2016-02-16 07:00 am
1 2016-02-16 06:30 pm
1 2016-02-17 07:15 am
2 2016-02-17 09:00 am
1 2016-02-17 11:30 am
1 2016-02-17 01:45 pm
1 2016-02-17 06:45 pm
2 2016-02-17 06:45 pm
Run Code Online (Sandbox Code Playgroud)
我需要的是计算一个列,确定IN或OUT办公室的员工.为此,我估计每个员工每天的第一条记录是IN,下一个OUT,下一个IN等等.
所以给定示例的结果应该是:
EmployeeID DateTime INOUT
---------- -------------------- -----
1 2016-02-16 07:00 am IN
1 2016-02-16 06:30 pm OUT
1 2016-02-17 07:15 am IN
2 2016-02-17 09:00 am IN
1 2016-02-17 11:30 am OUT
1 2016-02-17 01:45 pm IN
1 2016-02-17 06:45 pm …Run Code Online (Sandbox Code Playgroud) 我是LINQ的新手,我通常会用SQL解决这个问题,但现在我正在尝试用linq做同样的事情,我无法弄清楚这一点.我有3张桌子:
- 订单(OrderID,Number等)
- OrdersDetail(OrderID,ProductID,Quantity等)
- 产品(ProductID,代码,描述)
现在我需要一个包含产品匹配特定代码或描述字符串的订单列表.我的SQL将是:
SELECT * FROM Orders WHERE OrderID IN(SELECT OrderID FROM OrdersDetail WHERE ProductID IN(SELECT ProductID FROM Products WHERE Code LIKE 'FilterText%' OR Descripcion LIKE 'FilterText%'))
Run Code Online (Sandbox Code Playgroud)
我如何使用LINQ执行此操作?这也是最好的方法,特别是如果Products表很大?谢谢!
c# ×7
sql-server ×4
wpf ×4
linq ×3
sql ×2
xaml ×2
.net ×1
alter-table ×1
ansi-sql ×1
ef-core-3.0 ×1
modern-ui ×1
mvvm ×1
views ×1
wcf ×1