C# 如何在代码隐藏中使用绑定数据?
我有一系列的按钮来增加、显示和减少整数月份 编辑:一个更完整的示例 MainWindow.xamlC# 如何在代码隐藏中使用绑定数据?,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我有一系列的按钮来增加、显示和减少整数月份 编辑:一个更完整的示例 MainWindow.xaml <Window x:Class="ABC.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-na
<Window x:Class="ABC.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StackOverflow2"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ConvertMonth x:Key="ConvertMonth"/>
</Window.Resources>
<StackPanel>
<Button Content="+" Name="btnAddMonth" Click="btnAddMonth_Click"/>
<Button Content="{Binding ElementName=Month, Path=Value, Converter={StaticResource ConvertMonth}}"/>
<Button Content="-" Name="btnMinusMonth" Click="btnMinusMonth_Click"/>
<Button Content="Save Changes" Name=btnSaveChanges Click="btnSaveChanges_Click"/>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.IO;
namespace ABC
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
{
var mm = ConvertMonth.GetMonth();
DateTime date = new DateTime(mm);
}
public void btnAddMonth_Click(object sender, RoutedEventArgs e)
{
int h = Convert.ToInt16(btnMonth.Content.ToString());
if (h < 12)
{
int x = h + 1;
if (h < 9)
{
btnMonth.Content = x.ToString();
}
else
{
btnMonth.Content = x.ToString();
}
}
}
private void btnMinusMonth_Click(object sender, RoutedEventArgs e)
{
int h = Convert.ToInt16(btnMonth.Content.ToString());
if (h > 1)
{
int x = h - 1;
if (h < 11)
{
btnMonth.Content = x.ToString();
}
else
{
btnMonth.Content = x.ToString();
}
}
}
}
public class ConvertMonth : IValueConverter
{
private static int _month;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
object result = DependencyProperty.UnsetValue;
if (value is double)
result = DateTime.Now.AddMonths(System.Convert.ToInt32(value));
DateTime d = System.Convert.ToDateTime(result);
_month = d.Month;
return result;
}
public static int GetMonth()
{
return _month;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
根据您提供的内容,您可以更改此
public void btnSaveChanges_Click(object sender, RoutedEventArgs e)
{
//use *int* value of month here
//eg - updateMonth(/* value of {Binding Path=month} */);
}
到
这回答了你的问题。根据注释,似乎+和-按钮与应包含整数的按钮内容不交互。您需要告诉我们“月”在何处以及如何声明。“month”位于谁的路径上?“month”是int类型的本地声明…将进行编辑以显示更多细节。很公平,+按钮做什么?它是如何增加月份的?@GarryPass See update我有一个更新函数来调用代码,在代码中我需要得到int值。然后把numberOfMonths放在更新函数中……但是btnMonth.Content才是我真正想要的。
<Window x:Class="ABC.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StackOverflow2"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ConvertMonth x:Key="ConvertMonth"/>
</Window.Resources>
<StackPanel>
<Button Content="+" Name="btnAddMonth" Click="btnAddMonth_Click"/>
<Button Content="{Binding ElementName=Month, Path=Value, Converter={StaticResource ConvertMonth}}"/>
<Button Content="-" Name="btnMinusMonth" Click="btnMinusMonth_Click"/>
<Button Content="Save Changes" Name=btnSaveChanges Click="btnSaveChanges_Click"/>
</StackPanel>
</Window>
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.IO;
namespace ABC
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
{
var mm = ConvertMonth.GetMonth();
DateTime date = new DateTime(mm);
}
public void btnAddMonth_Click(object sender, RoutedEventArgs e)
{
int h = Convert.ToInt16(btnMonth.Content.ToString());
if (h < 12)
{
int x = h + 1;
if (h < 9)
{
btnMonth.Content = x.ToString();
}
else
{
btnMonth.Content = x.ToString();
}
}
}
private void btnMinusMonth_Click(object sender, RoutedEventArgs e)
{
int h = Convert.ToInt16(btnMonth.Content.ToString());
if (h > 1)
{
int x = h - 1;
if (h < 11)
{
btnMonth.Content = x.ToString();
}
else
{
btnMonth.Content = x.ToString();
}
}
}
}
public class ConvertMonth : IValueConverter
{
private static int _month;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
object result = DependencyProperty.UnsetValue;
if (value is double)
result = DateTime.Now.AddMonths(System.Convert.ToInt32(value));
DateTime d = System.Convert.ToDateTime(result);
_month = d.Month;
return result;
}
public static int GetMonth()
{
return _month;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
public void btnSaveChanges_Click(object sender, RoutedEventArgs e)
{
//use *int* value of month here
//eg - updateMonth(/* value of {Binding Path=month} */);
}
public void btnSaveChanges_Click(object sender, RoutedEventArgs e)
{
int numberOfMonths = Convert.ToInt32(btnMonth.Content);
}