简体   繁体   English

绑定C#和XAML错误

[英]Binding in C# and XAML error

I have the following code which I should put a random number in the content of a button.But rather everytime the content of the button is the same value 0 - This is part of my C# code- 我有以下的代码,我应该把一个随机数的content一button.But的,而每次按钮的内容是相同的值0 -这是我的一部分C#代码-

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.DataContext = this;
            this.NavigationCacheMode = NavigationCacheMode.Required;
        }
        public int RandomNumber { get; set; }
        public int GenerateRandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(5,40);
        }

  private void ButtonNo3(object sender, RoutedEventArgs e)
    {
        int ans = 0;
        Button obj = (Button)sender;
        var selectedButton = obj.Content.ToString(); 
        //some more code
    }

This is part of my XAML code in which my button should have a random number in its content- 这是我的XAML代码的一部分,其中我的按钮的内容应为随机数-

            <Button 
            x:Name="Button3"
            Content="{Binding Path=RandomNumber}"                           
            Click="ButtonNo3"
            HorizontalAlignment="Left" 
            Margin="0,118,0,0" 
            VerticalAlignment="Top" 
            Height="37" 
            Width="47"/>

SO my question is why is the Content of the Button3 always 0 when it should be random. 所以我的问题是,当Button3Content应该是随机的时,为什么它的Content总是为0

您不会在任何地方(至少不在显示的代码中)调用GenerateRandomNumber函数,因此RandomNumber属性将始终具有默认值0。

Following should do the trick 以下应该可以解决问题

public sealed partial class MainPage : Page,INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private int __RandomNumber;

    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = this;
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }
    public int RandomNumber 
    { 
        get{return _RandomNumber} 
        set
        {
            if (_RandomNumber != value){
              _RandomNumber = value;
              this.OnPropertyChanged("RandomNumber");//needed for binding
            }
        }; 
    }
    public int GenerateRandomNumber(int min, int max)
    {
        Random random = new Random();
        RandomNumber = random.Next(5,40); //changed this line, do the assign
        return RandomNumber;
    }

    private void ButtonNo3(object sender, RoutedEventArgs e)
    {
        int ans = 0;
        Button obj = (Button)sender;
        var selectedButton = obj.Content.ToString(); 
        //some more code
        GenerateRandomNumber(0,0);  //added : generate a random number
    }

There are multiple issues that i solved with this code. 我用此代码解决了多个问题。 You are never generating a random number, not assigning it either. 您永远不会生成随机数,也不分配它。 You are not implementing inotifypropertychanged so even if you were the generate a new number it would not be picked up by the binding. 您没有实现inotifypropertychanged,所以即使您生成一个新的数字,绑定也不会使用它。 The code above is not tested, it may have still some issues, but it should give you an idea. 上面的代码未经测试,可能仍然存在一些问题,但是应该可以使您有所了解。

As already stated you're not calling the GenerateRandomNumber method any where. 如前所述,您不会在任何地方调用GenerateRandomNumber方法。 You need to set the content property of the button to the value returned by the random number method. 您需要将按钮的content属性设置为随机数方法返回的值。

To get a different value every time put this code before you set your selectedButton variable in the click event. 要每次获得不同的值,请在设置click事件中的selectedButton变量之前放入此代码。

obj.Content = GenerateRandomNumber(5,40);
var selectedButton = obj.Content;

On another note. 另一个注意事项。 The parameters for your GenerateRandomNumber method are useless. GenerateRandomNumber方法的参数无用。 You're not using them.you should be doing this: 您没有使用它们。您应该这样做:

private int GenerateRandonNumber( int min, int max ) {
    Random random = new Random();
    return random.Next( min, max );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM