When is x:Reference in WPF resolved and why does XAML element order affect it?

后端 未结 3 1167
小蘑菇
小蘑菇 2021-01-11 20:25

x:Reference can not be resolved after I re-arrange elements in XAML.

Here I present a working code. Just move the DataGrid element so it comes after the button eleme

相关标签:
3条回答
  • 2021-01-11 21:12

    Excerpted from MSDN(http://msdn.microsoft.com/en-us/library/ee795380.aspx).

    x:Reference is a construct defined in XAML 2009. In WPF, you can use XAML 2009 features, but only for XAML that is not WPF markup-compiled. Markup-compiled XAML and the BAML form of XAML do not currently support the XAML 2009 language keywords and features.

    0 讨论(0)
  • 2021-01-11 21:20

    x:Reference must be avoided in WPF. Because this markup extension is a recent addition to the XAML language (2009). And it is not completely supported in WPF. Use ElementName in your Binding instead of x:Reference.

    <Binding Path="SelectedItem" 
             ElementName="dataGridElement"/>
    

    On MSDN.

    0 讨论(0)
  • 2021-01-11 21:22

    I suppose it is because your resources(Window.Resources) will be created first, before referenced instance exists. I would try to solve this through DataContext (ViewModel).

    <Window.DataContext>
            <yourNameSpace:YourViewModel x:Name="VieModName" />
        </Window.DataContext>
    <MenuItem Header="HeadrTxt" Command="{Binding CommandInViewModelCmd}" DataContext="{x:Reference Name=VieModName}" />
    
    0 讨论(0)
提交回复
热议问题