绑定更改未被推回到gui

时间:2017-08-04 14:46:07

标签: c# xaml uwp

我在某处使用以下代码(使用C#和UWP):

public static readonly DependencyProperty SupportsFineChannelProperty = DependencyProperty.Register(
            "SupportsFineChannel", typeof(bool), typeof(ChannelGroupView),
            new PropertyMetadata(default(bool)));

public bool SupportsFineChannel {
    get { return (bool) GetValue(SupportsFineChannelProperty); }
    set { SetValue(SupportsFineChannelProperty, value); }
}

在我的gui中我用它

<TextBox Text="{Binding FineChannel, Mode=TwoWay}" IsEnabled="{Binding SupportsFineChannel, Mode=TwoWay}" />
<CheckBox IsChecked="{Binding SupportsFineChannel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">Supports Fine Channel</CheckBox>

现在我有一个(据我的理解)异常行为。如果我在SupportFineChannel为false时加载框架,则禁用文本框。校验。如果我在bool为true时执行相同操作,则启用文本框。检查,绑定工作。

现在奇怪的是,如果我通过复选框更改TextBox,TextBox将不会更改其IsEnabled值。

使用调试器我确认:

  • CheckBox的Evaluated Binding值已更改
  • 绑定正确传播到后端的Variable,并按预期更改
  • 评估的文本框绑定值虽未记录更改的DepProp。

我已将绑定设置为OneWay和TwoWay,两者都不会改变此行为。为什么DepProp不会将更改传播回绑定?

修改 我只是用不同的属性测试它。事实是,当它改变时,没有依赖属性将值推回到gui ... srsly?

EDIT2 INotifyPropertyChanged确实可以工作.. UWP上的DepProps是否有一些我不知道的不同行为?

EDIT3 它比我想象的更容易创建一个repro样本。所以我的设置没有什么疯狂的.. 对于任何感兴趣的人,here it is

如果某个时候某个链接出现故障,或者您不相信我;

  1. 我创建了一个新的UWP空白项目,目标版本为10.0,15063,最低版本10.0 10586
  2. 创建DataView和ItemView

    public class DataView : DependencyObject {
        public static readonly DependencyProperty ItemStuffProperty = DependencyProperty.Register(
            "ItemStuff", typeof(ObservableCollection<ItemView>), typeof(DataView),
            new PropertyMetadata(default(ObservableCollection<ItemView>)));
    
        public ObservableCollection<ItemView> ItemStuff {
            get { return (ObservableCollection<ItemView>) GetValue(ItemStuffProperty); }
            set { SetValue(ItemStuffProperty, value); }
        }
    
        public DataView() {
            ItemStuff = new ObservableCollection<ItemView>();
        }
    }
    
    public class ItemView : DependencyObject {
        public static readonly DependencyProperty SupportsStuffProperty = DependencyProperty.Register(
            "SupportsStuff", typeof(bool), typeof(ItemView), new PropertyMetadata(default(bool)));
    
        public bool SupportsStuff {
            get { return (bool) GetValue(SupportsStuffProperty); }
            set { SetValue(SupportsStuffProperty, value); }
        }
    
        public static readonly DependencyProperty TextStuffProperty = DependencyProperty.Register(
            "TextStuff", typeof(string), typeof(ItemView), new PropertyMetadata(default(string)));
    
        public string TextStuff {
            get { return (string) GetValue(TextStuffProperty); }
            set { SetValue(TextStuffProperty, value); }
        }
    }
    
  3. 将DataView和示例项添加到您的代码隐藏

    public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
        "Data", typeof(DataView), typeof(MainPage), new PropertyMetadata(default(DataView)));
    
    public DataView Data {
        get { return (DataView) GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }
    
    
    public MainPage() {
        Data=new DataView();
        Data.ItemStuff.Add(new ItemView());
        this.InitializeComponent();
    }
    
  4. 将您的Datacontext设置为DataContext="{Binding RelativeSource={RelativeSource Self}, Path=Data}"

  5. 添加您的列表框

    <ListBox ItemsSource="{Binding ItemStuff}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBox Text="{Binding TextStuff}" IsEnabled="{Binding SupportsStuff, Mode=OneWay}" />
                    <CheckBox IsChecked="{Binding SupportsStuff, Mode=TwoWay}"> Supports Stuff</CheckBox>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    
  6. ???

  7. 利润!

0 个答案:

没有答案