在WPF数据绑定中,我知道你有DataContext一个告诉元素它将绑定到哪个数据以及ItemsSource哪个"绑定".
但是,例如在这个简单的例子中,它似乎没有ItemsSource做任何有用的事情,因为,除了绑定它之外,你还想要Element做什么DataContext呢?
<ListBox DataContext="{StaticResource customers}"
ItemsSource="{Binding}">
Run Code Online (Sandbox Code Playgroud)
在更复杂的例子中ItemsSource,你有路径和来源似乎正在侵占其领土DataContext.
ItemsSource="{Binding Path=TheImages, Source={StaticResource ImageFactoryDS}}"
Run Code Online (Sandbox Code Playgroud)
了解这两个概念的最佳方法是什么,以了解何时以及如何在各种编码方案中应用它们?
我需要添加许多类型为nullable int的变量.我使用了空合并运算符将它降低到每行一个变量,但我感觉有更简洁的方法来做到这一点,例如我不能以某种方式将这些语句链接在一起,我已经在其他之前看到过码.
using System;
namespace TestNullInts
{
class Program
{
static void Main(string[] args)
{
int? sum1 = 1;
int? sum2 = null;
int? sum3 = 3;
//int total = sum1 + sum2 + sum3;
//int total = sum1.Value + sum2.Value + sum3.Value;
int total = 0;
total = total + sum1 ?? total;
total = total + sum2 ?? total;
total = total + sum3 ?? total;
Console.WriteLine(total);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下MainView.xaml文件,它可以很好地用作MVVM菜单切换器.我有这些配对:
在我的MainViewModel中,我用两个ViewModel填充ObservableCollection,然后当用户单击Next按钮时,它调用MainViewModel中的NextPageCommand,它将一个新的ViewModel 切换出CurrentPageViewModel,然后用适当的View显示,效果很好.
我还有一个菜单填充了Observable集合中ViewModels的所有标题,这也很好用.
但是,每个MenuItem都有一个Command ="{Binding SwitchPageCommand}",它应该在MainViewModel上调用SwitchPageCommand,而不是在例如Page1ViewModel或Page2ViewModel上调用.
那么我怎么能在模板中指出不要绑定到当前的ViewModel,而是包含 ViewModel的ViewModel,例如:
PSEUDO-CODE:
<DataTemplate x:Key="CodeGenerationMenuTemplate">
<MenuItem
Command="{Binding <parentViewModel>.SwitchPageCommand}"
Header="{Binding Title}"
CommandParameter="{Binding Title}"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
这是MainViewModel:
<Window x:Class="TestMenu234.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestMenu234.Commands"
xmlns:vm="clr-namespace:TestMenu234.ViewModels"
xmlns:v="clr-namespace:TestMenu234.Views"
Title="Main Window" Height="400" Width="800">
<Window.Resources>
<DataTemplate x:Key="CodeGenerationMenuTemplate">
<MenuItem Header="{Binding Title}" Command="{Binding SwitchPageCommand}" CommandParameter="{Binding Title}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Page1ViewModel}">
<v:Page1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Page2ViewModel}">
<v:Page2View/>
</DataTemplate>
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="Code …Run Code Online (Sandbox Code Playgroud) 在查看ASP.NET MVC站点的示例时,我在视图中看到了很多嵌入式逻辑示例,例如:
<% if (customerIsAllowed)
{ %>
<p>nnn</p>
<p>nnn</p>
<p>nnn</p>
<p>nnn</p>
<p>nnn</p>
<% } else {%>
<p>nnn</p>
<p>nnn</p>
<p>nnn</p>
<p>nnn</p>
<p>nnn</p>
<% } %>
Run Code Online (Sandbox Code Playgroud)
虽然这对我来说似乎是错误的,因为它是我们试图摆脱ASP 3.0的那种东西,我甚至在一些播客中听到过"MVC框架的其余部分"中的"一点点逻辑是否正常"正在照顾我们在ASP 3.0中没有的结构.
是否有任何MVC约定指定视图中允许的逻辑类型和数量?
以下通用静态方法接受一个字符串并返回一个枚举.
因为我将ignoreCase参数设置为true,所以很好地忽略了大小写.
但是,我还想测试枚举是否存在,但执行此操作的enum.IsDefined方法似乎没有ignoreCase参数.
如何测试枚举是否被定义,并且在同一个忽略的情况下?
using System;
namespace TestEnum2934234
{
class Program
{
static void Main(string[] args)
{
LessonStatus lessonStatus = StringHelpers.ConvertStringToEnum<LessonStatus>("prepared");
ReportStatus reportStatus = StringHelpers.ConvertStringToEnum<ReportStatus>("finished");
Console.WriteLine(lessonStatus.ToString());
Console.WriteLine(reportStatus.ToString());
Console.ReadLine();
}
}
public static class StringHelpers
{
public static T ConvertStringToEnum<T>(string text)
{
if (Enum.IsDefined(typeof(T), text)) //does not have ignoreCase parameter
return (T)Enum.Parse(typeof(T), text, true);
else
return default(T);
}
}
public enum LessonStatus
{
Defined,
Prepared,
Practiced,
Recorded
}
public enum ReportStatus …Run Code Online (Sandbox Code Playgroud) 鉴于:
$this->objPHPExcelReader = PHPExcel_IOFactory::createReaderForFile($this->config['file']);
$this->objPHPExcelReader->setLoadSheetsOnly(array($this->config['worksheet']));
$this->objPHPExcelReader->setReadDataOnly(true);
$this->objPHPExcel = $this->objPHPExcelReader->load($this->config['file']);
Run Code Online (Sandbox Code Playgroud)
我可以迭代这样的行,但它很慢,即在3MB的Excel文件中,工作表有"EL"列,每行大约需要1秒:
foreach ($this->objPHPExcel->setActiveSheetIndex(0)->getRowIterator() as $row)
{
$dataset = array();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell)
{
if (!is_null($cell))
{
$dataset[] = $cell->getCalculatedValue();
}
}
$this->datasets[] = $dataset;
}
Run Code Online (Sandbox Code Playgroud)
当我像这样迭代时,它显着更快(在30秒内大约2000行),但我必须将字母例如"EL"转换为数字:
$highestColumm = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); // e.g. "EL"
$highestRow = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$number_of_columns = 150; // TODO: figure out how to get the number of cols as int
for ($row = 1; $row < $highestRow …Run Code Online (Sandbox Code Playgroud) 对于那些使用纯MVVM的人来说,如何在不回复代码的情况下处理ComboBox SelectionChanged事件?
我尝试了例如AttachedBehaviors,但不支持Event ="SelectedChanged":
<ComboBox>
<ComboBoxItem Content="Test1">
<c:CommandBehaviorCollection.Behaviors>
<c:BehaviorBinding Event="SelectionChanged"
Command="{Binding SelectedChanged}"
CommandParameter="MainBorder123"/>
</c:CommandBehaviorCollection.Behaviors>
</ComboBoxItem>
<ComboBoxItem Content="Test2"/>
<ComboBoxItem Content="Test3"/>
</ComboBox>
Run Code Online (Sandbox Code Playgroud) 我需要更改为以下代码,以便背景为红色,我尝试的两种方式都没有:
替代文字http://www.deviantsart.com/upload/1okq25l.png
XAML:
<Window x:Class="TestBackground88238.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBlock Text="{Binding Message}" Background="{Binding Background}"/>
<TextBlock Text="{Binding Message}">
<TextBlock.Background>
<SolidColorBrush Color="{Binding Background}"/>
</TextBlock.Background>
</TextBlock>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System.Windows;
using System.ComponentModel;
namespace TestBackground88238
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: Background
private string _background;
public string Background
{
get
{
return _background;
}
set
{
_background = value;
OnPropertyChanged("Background");
}
}
#endregion
#region ViewModelProperty: Message
private string _message;
public string Message
{
get
{
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试减少发送到我的apache服务器日志的警告.
一个警告是:
已弃用调用时传递引用.
这是我很难想象为什么这个被废弃了,因为它是这样一个有用的编程功能,基本上我这样做:
public function takeScriptsWithMarker(&$lines, $marker) {
...
}
Run Code Online (Sandbox Code Playgroud)
我调用这个函数重复从它得到结果并处理它们,但也让数组$ lines通过重复发送到这个方法来建立.
因此,由于不推荐使用call-by引用,获得此模式功能的"可接受方式"是什么:即将字符串数组发送到方法中,让方法更改它们,然后继续使用阵列?
wpf ×4
c# ×3
php ×3
data-binding ×2
mvvm ×2
xaml ×2
asp.net-mvc ×1
background ×1
binding ×1
datacontext ×1
enums ×1
events ×1
itemssource ×1
jquery ×1
netbeans ×1
nullable ×1
phpexcel ×1
views ×1