我正在编写一个GUI应用程序,我需要启用任意对象的编辑属性(它们的类型仅在运行时才知道).
我决定使用PropertyGrid控件来启用此功能.我创建了以下类:
[TypeConverter(typeof(ExpandableObjectConverter))]
[DefaultPropertyAttribute("Value")]
public class Wrapper
{
public Wrapper(object val)
{
m_Value = val;
}
private object m_Value;
[NotifyParentPropertyAttribute(true)]
[TypeConverter(typeof(ExpandableObjectConverter))]
public object Value
{
get { return m_Value; }
set { m_Value = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
当我得到一个我需要编辑的对象实例时,我为它创建了一个Wrapper并将其设置为所选对象:
Wrapper wrap = new Wrapper(obj);
propertyGrid.SelectedObject = wrap;
Run Code Online (Sandbox Code Playgroud)
但是我遇到了以下问题 - 只有当obj的类型是某种自定义类型(即我自己定义的类,或者内置的复杂类型)时才能按预期工作,但是当obj是基元时则不行.
例如,如果我定义:
[TypeConverter(typeof(ExpandableObjectConverter))]
public class SomeClass
{
public SomeClass()
{
a = 1;
b = 2;
}
public SomeClass(int a, int b)
{
this.a = a;
this.b = b;
}
private …Run Code Online (Sandbox Code Playgroud) 我没经验,特别是在MVVM,但尝试使用ReactiveUI,我不理解我发现的演示ReactiveCommand的示例.我曾经使用过一次ICommand/DelegateCommand,但这是不同的,我没有得到它.
我想做的事情非常简单.单击视图中的按钮,然后在视图模型中执行该方法.我发现的所有例子都涉及IObservable <>,我不明白,因为它们没有针对我的总noob的解释.
基本上,我正在尝试将其用作学习体验,而我理想的做法是将xaml中按钮的Command属性绑定到命令(但是这有效,我不知道),这会导致一个方法执行.没有集合,我只是传递一个int变量.
谢谢您的帮助.对此,我真的非常感激.
编辑 - 使用Paul Betts的建议下面显示代码:
C#
public ReactiveCommand AddToDailyUsed { get; protected set; }
public MainPageVM()
{
Initialize();
AddToDailyUsed = new ReactiveCommand();
AddToDailyUsed.Subscribe(AddToTodayUsedAction => this.AddToDailyUsedExecuted());
}
private object AddToDailyUsedExecuted()
{
MessageBox.Show("AddToDailyUsedAction");
return null;
}
private void AddToDailyUsedAction(object obj)
{
MessageBox.Show("AddToDailyUsedAction");
}
Run Code Online (Sandbox Code Playgroud)
XAML
<Button Content="{Binding Strings.add, Source={StaticResource LocalStrings}}"
Command="{Binding AddToTodayUsed}"
Margin="-5,-10, -10,-10"
Grid.Row="3"
Grid.Column="2" />
Run Code Online (Sandbox Code Playgroud)
显然我错过了一些东西.我在AddToDailyUsedExecuted和AddToDailyUsedAction方法中插入了断点,但它们永远不会到达.
编辑构造函数以查看视图后面的代码:
MainPageVM mainPageVM = new MainPageVM();
public MainPage()
{
InitializeComponent();
Speech.Initialize();
DataContext = mainPageVM;
ApplicationBar = new ApplicationBar();
TaskRegistration.RegisterScheduledTask();
this.Loaded …Run Code Online (Sandbox Code Playgroud) 我正在学习WPF MVVM模式.我被困在Binding CurrentCell的datagrid.基本上我需要当前单元格的行索引和列索引.
<DataGrid AutoGenerateColumns="True"
SelectionUnit="Cell"
CanUserDeleteRows="True"
ItemsSource="{Binding Results}"
CurrentCell="{Binding CellInfo}"
Height="282"
HorizontalAlignment="Left"
Margin="12,88,0,0"
Name="dataGrid1"
VerticalAlignment="Top"
Width="558"
SelectionMode="Single">
Run Code Online (Sandbox Code Playgroud)
这是我的ViewModel
private User procedureName = new User();
public DataGridCell CellInfo
{
get { return procedureName.CellInfo; }
//set
//{
// procedureName.CellInfo = value;
// OnPropertyChanged("CellInfo");
//}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模特
private DataGridCell cellInfo;
public DataGridCell CellInfo
{
get { return cellInfo; }
//set
//{
// cellInfo = value;
// OnPropertyChanged("CellInfo");
//}
}
Run Code Online (Sandbox Code Playgroud)
而在我的ViewModel CellInfo中总是如此null.我没能获得从价值currentcell在datagrid.请让我知道一种 …
我正在开发一个基于 MVVM 的 WPF 应用程序。我想将字符串列表绑定到列标题,即,如果列表包含“abc”、“xyz”、“pqr”,那么我DataGrid应该有三列,标题为 abc、xyz、pqr。这是我将数据网格绑定到的类。行存储在ObservableCollection<List<string>>其中的每个元素ObservableCollection是形成行的单元格的字符串列表中。
public class Resource
{
private ObservableCollection<string> columns;
public ObservableCollection<string> Columns
{
get
{
return columns;
}
set
{
columns = value;
}
}
private ObservableCollection<List<string>> row;
public ObservableCollection<List<string>> Row
{
get
{
return row;
}
set
{
row = value;
}
}
public Resource()
{
List<string> a = new List<string>();
a.Add("1");
a.Add("2");
List<string> b = new List<string>();
b.Add("11");
b.Add("21");
Row = new ObservableCollection<List<string>>();
Row.Add(a);
Row.Add(b);
Columns = new …Run Code Online (Sandbox Code Playgroud) 我有一个.Net 4.5应用程序正在转向基于WPF的RxUI(在撰写本文时保持最新,6.0.3).我有一个文本字段,应该作为一个过滤器字段,具有相当常见的油门等东西,这是首先发生反应的部分原因.
这是我班级的相关部分.
public class PacketListViewModel : ReactiveObject
{
private readonly ReactiveList<PacketViewModel> _packets;
private PacketViewModel _selectedPacket;
private readonly ICollectionView _packetView;
private string _filterText;
/// <summary>
/// Gets the collection of packets represented by this object
/// </summary>
public ICollectionView Packets
{
get
{
if (_packets.Count == 0)
RebuildPacketCollection();
return _packetView;
}
}
public string FilterText
{
get { return _filterText; }
set { this.RaiseAndSetIfChanged(ref _filterText, value); }
}
public PacketViewModel SelectedPacket
{
get { return _selectedPacket; }
set { this.RaiseAndSetIfChanged(ref …Run Code Online (Sandbox Code Playgroud) 我正在使用nAudio Library来捕获麦克风输入.但我遇到了一个问题.我正在使用nAudio示例应用程序中的代码(我稍微修改过).代码生成基于麦克风输入的WAV文件并将其呈现为波形.这是代码.
private void RenderFile()
{
SampleAggregator.RaiseRestart();
using (WaveFileReader reader = new WaveFileReader(this.voiceRecorderState.ActiveFile))
{
this.samplesPerSecond = reader.WaveFormat.SampleRate;
SampleAggregator.NotificationCount = reader.WaveFormat.SampleRate/10;
//Sample rate is 44100
byte[] buffer = new byte[1024];
WaveBuffer waveBuffer = new WaveBuffer(buffer);
waveBuffer.ByteBufferCount = buffer.Length;
int bytesRead;
do
{
bytesRead = reader.Read(waveBuffer, 0, buffer.Length);
int samples = bytesRead / 2;
double sum = 0;
for (int sample = 0; sample < samples; sample++)
{
if (bytesRead > 0)
{
sampleAggregator.Add(waveBuffer.ShortBuffer[sample] / 32768f);
double sample1 = waveBuffer.ShortBuffer[sample] / 32768.0; …Run Code Online (Sandbox Code Playgroud) public T Foo<T, U>(U thing) where T : new()
{
return new T();
}
Run Code Online (Sandbox Code Playgroud)
当没有new()约束时,我理解它是如何工作的.JIT编译器看到T,如果它是引用类型,则使用代码的对象版本,并专门针对每个值类型的情况.
如果你有一个新的T(),它是如何工作的?它在哪里寻找?
当itemsource以编程方式更改时,我无法成功更新我的WPF Datagrid.
XAML
<DataGrid Name="ReaderGrid" ItemsSource="{Binding myData}" Height="Auto" Width="Auto" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" IsReadOnly="True" GridLinesVisibility="None">
<DataGrid.Columns>
<DataGridTextColumn Header="Count" Width="*" FontSize="14" Binding="{Binding count}" />
<DataGridTextColumn Header="Total" Width="*" FontSize="14" Binding="{Binding total}" />
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
XAML.CS(代码隐藏)
public partial class MainWindow : Window
{
public ObservableCollection<obj> myData { get; set; }
public MainWindow()
{
InitializeComponent();
myData = new ObservableCollection<obj>();
InitializeMyData();
Run Code Online (Sandbox Code Playgroud)
最后一个函数(InitializeMyData())只是用测试信息填充myData.
最后,对象只是一些测试类
public class obj
{
public int count { get; set; }
public double total { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
代码AS-IS不起作用,除了WPF中的数据网格为空之外没有错误 …
我有一个根据用户在我的 Datagrid 行中输入的数字更新的总计变量。我想在更改每一行单元格时更新该值。这是我到目前为止所做的:
private void QuotationDG_CellEditEnding(object sender,
DataGridCellEditEndingEventArgs e)
{
int ColumnIndex = e.Column.DisplayIndex;
Double amount= Double.Parse(((TextBox)e.EditingElement).Text);
Cat1SubTotal += amount;
GrandTotal += amount;
}
Run Code Online (Sandbox Code Playgroud)
每次用户输入新值时,此代码都会将金额相加。但是,如果用户编辑了现有值,那么这将在不删除旧值的情况下添加新值,因此将显示不正确的总数。
我需要做这样的事情:
Cat1SubTotal += (NewValue-OriginalValue)
Run Code Online (Sandbox Code Playgroud) 我无法将List绑定到DataGrid.它应该尽可能简单.我是WPF的新手,这是我的个人教育.
我有一个View(编辑器),ViewModel(VMText)和一个Data(JustText)类.
我的来源到目前为止:
JustText.cs
namespace Model
{
public class Text
{
private string _code;
public string Code
{
get { return _code; }
set { _code = value; }
}
public Text()
{
_code = "Hello World!\nHow you doin'?";
}
}
}
Run Code Online (Sandbox Code Playgroud)
VMText.cs
namespace ViewModel
{
public class VMText
{
private Model.Text _code;
public List<string> Code
{
get { return new List<string>(_code.Code.Split('\n')); }
set { _code.Code = System.String.Join("\n", value.ToArray()); }
}
private View.Editor editor;
public VMText(View.Editor editor)
{
_code = …Run Code Online (Sandbox Code Playgroud) c# ×9
wpf ×6
datagrid ×5
mvvm ×3
binding ×2
reactiveui ×2
xaml ×2
audio ×1
clr ×1
controls ×1
generics ×1
microphone ×1
naudio ×1
propertygrid ×1
recording ×1
silverlight ×1
xunit.net ×1