我在XAML 3中定义了菜单项(使用WPF-MDI):
<MenuItem Header="_Generic" Name="Generic" ToolTip="Generic Visual Studio designer theme"
Command="{Binding Path=SelectGenericTheme}"/>
<MenuItem Header="_Luna" Name="Luna" ToolTip="Blue Windows XP theme"
Command="{Binding Path=SelectLunaTheme}"/>
<MenuItem Header="_Aero" Name="Aero" ToolTip="Windows Vista/7 theme"
Command="{Binding Path=SelectAeroTheme}"/>
Run Code Online (Sandbox Code Playgroud)
以及ViewModel中命令和当前选择的定义:
public enum ESelectedTheme
{
Generic,
Luna,
Aero
}
ESelectedTheme _selectedTheme;
ICommand _selectGenericThemeCommand;
public ICommand SelectGenericThemeCommand
{
get { return _selectGenericThemeCommand ?? (_selectGenericThemeCommand = new RelayCommand(param => SelectGenericTheme(),
param => true)); }
}
void SelectGenericTheme()
{
_selectedTheme = ESelectedTheme.Generic;
}
ICommand _selectLunaThemeCommand;
public ICommand SelectLunaThemeCommand
{
get
{
return _selectLunaThemeCommand ?? …Run Code Online (Sandbox Code Playgroud) 我知道如何从 XAML 中的字符串资源获取资源:
{Binding OK, Source={StaticResource LocStrings}}
Run Code Online (Sandbox Code Playgroud)
但是,我想直接在 C# 中使用本地化字符串,例如
someString = Resources["LocStrings"]["StringId"];
Run Code Online (Sandbox Code Playgroud)
但这不能编译。
我有一个字符串:
string s = "abc";
Run Code Online (Sandbox Code Playgroud)
我想将第二个字符更改为'd',因此它将是"adc"我认为这样可行:
s[1] = 'd';
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
Property or indexer 'string.this[int] 'cannot be assigned to -- it is read only
Run Code Online (Sandbox Code Playgroud)
我怎样才能完成这个简单的任务(不使用相当复杂的子串方法?
我目前正忙于将应用程序重写为MVVM(分阶段,因为它需要做很多工作).
目前,我可以根据模型视图中的更改填充列表视图和控件,并且列表视图选择也正确耦合(至少我知道如何正确地完成它).
但是,我想知道模型中的变化将反映在模型视图中(因此在视图中).我应该在模型中的所有项目上添加INotifyPropertyChange接口并订阅模型视图(如果我想订阅不在模型层次结构"顶部"的项目,甚至可能像链一样)?
例如,我有一个包含列表B等的列表A.我是否需要在列表A中订阅列表B的属性更改,并在模型视图中订阅列表A?并取消订阅是否进行了另一个列表选择(导致大量取消订阅和新订阅项目)?
感谢您阅读/回答.
我使用C#,MVVM,WPF和Resharper.
使用以下代码时:
public bool CombiBanksSelected
{
get { return _selectedBanksType == ESelectedBanksType.CombiBanks; }
set
{
Run Code Online (Sandbox Code Playgroud)
我得到了Resharper的警告:将set accessor设为私有.
将set方法设为私有时,我得到一个InvalidOperationException:TwoWay或OneWayToSource绑定不能对'PcgTools.ViewModels.PcgViewModel'类型的只读属性''CombiBanksSelected''起作用.
当然我可以通过添加以下内容来抑制它:
public bool CombiBanksSelected
{
get { return _selectedBanksType == ESelectedBanksType.CombiBanks; }
// ReSharper disable MemberCanBePrivate.Global
set
// ReSharper restore MemberCanBePrivate.Global
{
Run Code Online (Sandbox Code Playgroud)
但这并不好看而且感觉不好.这个问题有更好的替代方案或解决方案吗?
根据答案,我应该更改XAML代码.我的是:
<UserControl x:Class="PcgTools.PcgWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:PcgTools.ViewModels" Height="Auto" Width="Auto"
Loaded="Window_Loaded">
<Grid>
...
<RadioButton Content="_Programs" Height="16" HorizontalAlignment="Left" Margin="12,12,0,0" Name="radioButtonPrograms" VerticalAlignment="Top"
IsChecked="{Binding Path=ProgramBanksSelected}" IsEnabled="{Binding Path=ProgramsEnabled}"/>
<RadioButton Content="_Combis" Height="16" HorizontalAlignment="Left" Margin="85,12,0,0" Name="radioButtonCombis" VerticalAlignment="Top"
IsChecked="{Binding Path=CombiBanksSelected}" IsEnabled="{Binding Path=CombisEnabled}"/>
Run Code Online (Sandbox Code Playgroud)
我有两个(和更多)绑定到IsChecked的属性的Resharper问题(上面代码中的ProgramBanksSelected和CombiBanksSelected).
在答案中显示我应该使用DataTemplate,但我仍然无法弄清楚究竟是多么准确(没有使用MVVM light的Locator).
我应该如何使用数据上下文/模板?
我有一个类(Patch),我想要排序,所以我实现了IComparer.
但是,它需要根据用户的需要进行排序,例如: - key1,key2,key3 - key1,key3,key2
对于每个密钥比较,我写了一个IComparer类,但是,我想知道如何实现它的连接.即排序时我只能传递一个IComparer实例.
或者我应该为每种完整排序创建一个IComparer类,即IComparerKey1Key2Key3,IComparerKey1Key3Key2等?
我有一个在两个平台之间选择的头文件:
#pragma once
#ifdef _WINDOWS
#define PAR_CLASS TestPar
#define PAR_INCLUDE_FILE "TestPar.h"
#else
#define PAR_CLASS Par
#define PAR_INCLUDE_FILE "Par.h"
#endif
Run Code Online (Sandbox Code Playgroud)
这样,我可以使用以下行包括头文件:
#include "ClassNames.h"
#include PAR_INCLUDE_FILE
Run Code Online (Sandbox Code Playgroud)
但是,我希望有更多的类,对于PAR_CLASS和PAR_INCLUDE_FILE,唯一的不同是“和.h我想要的是在PAR中使用PAR_CLASS #include,例如:
#include "PAR_CLASS.h"
Run Code Online (Sandbox Code Playgroud)
但这是行不通的...这可能吗?
我希望它可以在Visual Studio(2019)和Arduino IDE中使用。
我有以下代码:
var str = "ABC";
var n = 7;
var output = String.Format("{0,n}", str);
Run Code Online (Sandbox Code Playgroud)
这应该输出字符串
" ABC"
Run Code Online (Sandbox Code Playgroud)
我该如何更改上面的行?
是否有一个WPF控件看起来像一个滑块(或者可能是Slider本身),它既有最小值又有最大值而不是单个值让用户选择一个范围?
我正在使用 WrapPanel 并且因为我想节省空间,所以我想根据某些功能删除(而不是隐藏)一些控件(其中一些是组框)。
我应该使用什么命令来删除控件?