出于某种原因,我遇到问题通过我的ViewModel在MVVM WPF应用程序中绑定自定义用户控件.基本控件是带有三个文本框的日期输入表单.我使用codebehind为usercontrol捕获textchange事件,因此进行了一些操作.由于某种原因,添加绑定到属性永远不会触发.
用户控件的XAML:
<UserControl x:Class="MYLibray.DateBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="800">
<StackPanel>
<Border CornerRadius="10" Height="200" BorderBrush="Gray" Background="Gray">
<StackPanel Orientation="Horizontal" OpacityMask="{x:Null}" HorizontalAlignment="Center">
<TextBox Name="txtMonth" Height="100" Width="90" BorderThickness="0,0,0,5" Background="{x:Null}" Text="" FontSize="72" TextChanged="TextChanged">
<TextBox.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FF000000" Offset="1"/>
</LinearGradientBrush>
</TextBox.BorderBrush>
</TextBox>
<TextBlock Text="/" FontSize="72" Height="100" Width="50" />
<TextBox x:Name="txtDay" Height="100" Width="90" Background="{x:Null}" BorderThickness="0,0,0,5" VerticalAlignment="Stretch" FontSize="72" TextChanged="TextChanged">
<TextBox.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FF000000" Offset="1"/>
</LinearGradientBrush>
</TextBox.BorderBrush>
</TextBox>
<TextBlock Text="/19" FontSize="72" Height="100" Width="Auto" />
<TextBox x:Name="txtYear" Height="100" Width="90" Background="{x:Null}" …Run Code Online (Sandbox Code Playgroud) 我有一个函数,它根据输入字符串返回一个新类.
private classX GetData(string id)
{
if (id == "-string1")
return new class1(this, @"path");
if (id == "-string2")
return new class2(this, @"path");
if (id == "-string3")
return new class3(this, @"path");
// ...
return null;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我希望有一个条件(例如"-all")逐个返回每个类.我不知道该怎么做.有没有办法逐一归还这些课程?因为我知道它不能与'return'一起使用,因为它结束了代码,所以其余部分无法访问.
我已经将可空类型的任务转换变量设置为不可空,如果它可以为空并且重新定义类型的默认值(如果需要的话).我为它编写了泛型静态类,但遗憾的是,它的工作速度很慢.该类将用于从数据库读取数据到模型,因此性能非常重要.这是我的课.
public static class NullableTypesValueGetter<T> where T : struct
{
#region [Default types values]
private static DateTime currentDefaultDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
#endregion
#region [Default types to string values]
private const string nullableDateTimeValue = "DateTime?";
#endregion
public static T GetValue(dynamic value)
{
if (Nullable.GetUnderlyingType(value.GetType()) != null) //if variable is nullable
{
var nullableValue = value as T?;
//if variable has value
if (nullableValue.HasValue)
{
return nullableValue.Value;
}
//if hasn't
else
{
var result = default(T);
//extensionable code, determination default …Run Code Online (Sandbox Code Playgroud) 我正在尝试了解 DependencyProperty。为此,我想创建一个UserControl显示列表的新对象。
此列表的位置必须作为属性存在于父级中。为此,我只有MainWindow, MainWindowViewModel (这些是父级)和UserControl(子级)(目前正在使用隐藏代码)。
在我的主窗口中,我有
<Grid>
<uc:RecentList MessageList="{Binding Messages}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
而在后面的代码中
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
Run Code Online (Sandbox Code Playgroud)
和视图模型
public MainWindowViewModel()
{
this.Messages = new ObservableCollection<string>();
this.Messages.Add("Item 1");
this.Messages.Add("Item 2");
this.T = "hi";
}
public ObservableCollection<string> Messages { get; set; }
Run Code Online (Sandbox Code Playgroud)
在UserControl我有
<Grid>
<ListView ItemsSource="{Binding MessageList}"></ListView>
<TextBlock Text="I'm such text to verify this control is showing" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
而背后的代码是
public static readonly DependencyProperty MessageListProperty =
DependencyProperty.Register(
"MessageList", typeof(IEnumerable<string>), …Run Code Online (Sandbox Code Playgroud)