订阅了dependencypropertychangedevent [英] Subscribed to dependencypropertychangedevent

查看:31
本文介绍了订阅了dependencypropertychangedevent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey gusy,
In wpf & C#, i have a user control (name: "Package_Combo") and inside it i have a label. 
the label content getting its value from DependencyProperty called LabelContentProperty.
the LabelContentProperty is register to a string property called LabelContent: 

public static readonly DependencyProperty LabelContentProperty = DependencyProperty.Register(LabelContent, typeof(string), 
typeof(Package_Combo), new PropertyMetadata(null, new PropertyChangedCallback(LabelContentChanged)));

public string LabelContent
{
    get { return (string)GetValue(LabelContentProperty); }
    set { SetValue(LabelContentProperty, value); }
}

public string LabelContent
{
     get { return (string)GetValue(LabelContentProperty); }
     set { SetValue(LabelContentProperty, value);}
}

private static void LabelContentChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
	//code goes here...
}

In xaml, the label content set like that:
 <Label x:Name="lbl" Content="{Binding ElementName=PackageCombo, Path=LabelContent}"/>

Now here is the problem:
Whenever the content is changes, its fire the LabelContentChanged event.
In that event, i want to fire a non-static function ("GetComboDataSource") that initial the itemsource of a combobox that also is in the same user control.
I cant set "GetComboDataSource" to static beacuse the function uses a dispather to handle web-api result. this is the logic:

public DataTable GetComboDataSource()
        {
            try
            {
                HttpClient client = new HttpClient();
                HttpRequestMessage request;

                string controller, function;
                string[] s = ControllerAndFunc.Split('-');
                controller = s[0];function = s[1];
                string serviceUrl = GV.ServiceUrl + controller + @"/" + function;

                var paramsList = new List<ParamsList>();
                paramsList.Add(new ParamsList { ParamName = ParameterConsts.language, ParamValue = Utils.GetLanguageId().ToString() });

                serviceUrl = Utils.ServiceUrlBuilder(paramsList, serviceUrl);
                request = new HttpRequestMessage(HttpMethod.Get, serviceUrl);
                Task<HttpResponseMessage> responseTask = client.SendAsync(request);

                DataTable result = null;
                string displayMember = DisplayMemberPath;
                string selectedValue = SelectedValuePath;
                Task continuation = responseTask.ContinueWith(x => result = HandleResult(x, paramsList, displayMember, selectedValue));
                Task.WaitAll();
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
        private DataTable HandleResult(Task<HttpResponseMessage> httpTask, List<ParamsList> userParamsList, string displayMember, string selectedValue)
        {
            try
            {
                Task<string> task = httpTask.Result.Content.ReadAsStringAsync();
                string result = string.Empty;
                Task continuation = task.ContinueWith(t =>
                {
                    result = t.Result;
                    Data = (DataTable)JsonConvert.DeserializeObject(result, (typeof(DataTable)));
                    Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() =>
                    {
                        this.DataContext = this;
                        cmb.DisplayMemberPath = displayMember;
                        cmb.SelectedValue = selectedValue;
                    }));
                });
                Task.WaitAll();
                return Data;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

How can i call GetComboDataSource within LabelContentChanged without creating object reference?





我的尝试:





What I have tried:

i have tried working with delegates and dispatcher but without any luck

推荐答案

DependencyObject LabelContentChanged 事件处理程序的c $ c>参数将是属性已更改的控件实例。您只需将其强制转换为适当的类型,然后调用您的方法:

The DependencyObject parameter passed to the LabelContentChanged event handler will be the instance of your control for which the property has changed. You just need to cast it to the appropriate type, and call your method:
private static void LabelContentChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
    YourControl theControl = (YourControl)dependencyObject;
    theControl.OnLabelContentChanged(eventArgs);
}

private void OnLabelContentChanged(DependencyPropertyChangedEventArgs eventArgs)
{
    ...
}


这篇关于订阅了dependencypropertychangedevent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆