开发者

Databinding IsEnabled of button in a xaml file

I want to bind a buttons IsEnabled property to a bool value that is retunred by a WCF web service.

Following is the details...

I have a datagrid in one of my xaml file. In this grid i have a button as part of DataGridTemplateColumn. now i want to data bind the isenable of this button to a bool开发者_Go百科 value returned by the web service.

Can't we done something very simple like..

<Button x:Name="btnUpdRequest" Content="Update" Click="btnUpdRequest_Click" Margin="2" IsEnabled="{Binding isUpdateable}" />

where isUpdateable is one of the value returned by the web service.

Thanks..


What you're describing is very doable, but...first of all, make sure that isUpdatable is a public property that is (a) on the current DataContext, (b) properly raises a PropertyChanged event when it gets updated (or is a DependencyProperty), and (c) is set to the desired initial value. Your web service call will be asynchronous (web service calls in Silverlight are Async) so if you're using anything other than the vanilla Async Model (where you hook an event prior to making your call and set your value within the event handler - and the event handler is guaranteed to be on the UI thread) you may need to marshal your value back to the UI thread to prevent a nasty exception.

Just remember that because of the async nature of the call, the button will update to its desired value only after the call returns, so set the initial value correctly (enabled or disabled, depending on your needs.)

Here's a quick & dirty sample (the service call merely toggles the value it receives as its parameter):

Codebehind:

public partial class MainPage : UserControl, INotifyPropertyChanged
{
    public MainPage()
    {
        InitializeComponent();
    }

    private bool _isUpdatable;
    public Boolean IsUpdatable
    {
        get { return _isUpdatable; }
        set
        {
            if (_isUpdatable != value)
            {
                _isUpdatable = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsUpdatable"));
            }

        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void CallTheService_Click(object sender, RoutedEventArgs e)
    {
        var serviceProxy = new Service1Client();
        serviceProxy.ToggleCompleted += new EventHandler<ToggleCompletedEventArgs>(serviceProxy_ToggleCompleted);
        serviceProxy.ToggleAsync(IsUpdatable);
    }

    private void serviceProxy_ToggleCompleted(object sender, ToggleCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            IsUpdatable = e.Result;
        }
    }
}

XAML:

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding ElementName=window1}">
    <Button IsEnabled="{Binding IsUpdatable}" Content="Foo" Height="100" VerticalAlignment="Top"/>
    <Button Content="Toggle Foo" Height="100" VerticalAlignment="Bottom" Click="CallTheService_Click" />
</Grid>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜