开发者

How do I bind a checkbox's checked function in xaml/c#

Hey, and thanks for looking.

I have the following in my MainPage.xaml

<TextBlock x:Name="ItemName1" Text="{Binding EventName, Mode=TwoWay}" Style="{StaticResource PhoneTextNormalStyle}" />
<CheckBox Margin="0,0,0,0" x:Name="Checkbox1" IsChecked="{Binding isCheckboxChecked, Mode=TwoWay}" Checked="EditData_Click"/>

I am able to control the value of the checkbox via the binding, without any trouble. How can I make it so that the checked handler 'EditData_Click' can recognize the value of the corresponding EventName, and run some other code path accordingly. I have a switch case in mind, something like:

switch (EventName_string)
{
    case "Dogs": // do something
  bre开发者_如何学Cak;
    case "Cats": // do something else
}


do it in your view model like so:

MyViewModel : BindableObject // or whatever your base class that implement INotifyPropertyChanged is
{
    private string eventName;

    public string EventName
    { 
        get{ return eventName; }
        set
        {
            if(value != eventName)
            {
                eventName = value;
                FirePropertyChanged(value, "EventName");
            }
        }
    }

    private bool checkBoxIsChecked;

    public bool CheckBoxIsCheck
    { 
        get{ return eventName; }
        set
        {
            if(value != eventName)
            {
                eventName = value;
                FirePropertyChanged(value, "CheckBoxIsCheck");
                DoExtraProcessing();
            }
        }
    }   

    private void DoExtraProcessing()
    {
        switch (EventName)
        {
           case "Dogs": // do something
           break;
           case "Cats": // do something else
           break;
        }
    }
}


Within MainPage.xaml you can switch on either ItemName1.Text or EventName.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜