绑定到静态类/字段

时间:2017-06-05 18:45:18

标签: c# wpf xaml data-binding static-class

我想摆脱一些奇怪的错误。我为我的一个应用程序创建了一个自定义的.cur Cursor。我已将自定义光标加载到图像文件夹中的项目中,将其设置为我的资源,并创建一个静态类以打开媒体流,并将光标传递给窗口。从我的XAML中我有以下代码:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myCtrls="clr-namespace:UserControlsLibrary;assembly=UserControlsLibrary"
        xmlns:pos="clr-namespace:POSystem"
        x:Name="Main" x:Class="POSystem.MainWindow" 
        Title="Purchase Order"
        Cursor="{Binding Source={x:Static pos:POProperties.LTC_Cursor}, Mode=OneWay}"
        Height="850" Width="1100" Background="{StaticResource BackgroundImage}" 
        Icon="Images/logo_only_Xns_icon.ico">
    <Grid x:Name="TheGrid" Focusable="True" 
        MouseDown="ClearFocus_OnClick" Background="Transparent">
        <StackPanel x:Name="Panelicus">
        </StackPanel>
    </Grid>
</Window>

我找到了这种绑定到静态类here的方法,它在技术上有效。问题是即使我已经构建了项目甚至成功运行代码,它也会显示一个无效的标记错误:

  

名称&#34; POProperties&#34;在命名空间中不存在   &#34; CLR-名称空间:POSystem&#34;

但是,此错误不正确,但导致我无法在Visual Studio中使用XAML设计器。

POProperties代码:

namespace POSystem
{
  public static class POProperties
  {
    private static Cursor _ltc_cursor;
    public static Cursor LTC_Cursor
    {
      get => _ltc_cursor ?? new Cursor(new System.IO.MemoryStream(Properties.Resources.LTC_Custom_Cursor));
      set => _ltc_cursor = value;
    }
  }
} 

1 个答案:

答案 0 :(得分:0)

我对尝试使用静态属性方法感到不耐烦,所以我只是在一个非静态类中对POProperties进行静态实例化,在实例化之外没有静态项。代码如下:

窗口:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myCtrls="clr-namespace:UserControlsLibrary;assembly=UserControlsLibrary"
        x:Name="Main" x:Class="POSystem.MainWindow" Title="Purchase Order" Cursor="{Binding LTC_Cursor, Mode=OneWay}"
        Height="850" Width="1100" Background="{StaticResource BackgroundImage}" Icon="Images/logo_only_Xns_icon.ico">
    <Grid x:Name="TheGrid" Focusable="True" MouseDown="ClearFocus_OnClick" Background="Transparent">
        <StackPanel x:Name="Panelicus">
            <TextBox x:Name="PONumLabel" Style="{StaticResource TBLabelStyle}" Width="250" Text="PO Number:"/>
            <myCtrls:DDTextBox x:Name="ItemsBox" Width="300" VerticalAlignment="Top" HorizontalAlignment="Left" MinHeight="25" Panel.ZIndex="1"/>
        </StackPanel>
    </Grid>
</Window>

窗口背后的代码:

namespace POSystem
{
  public partial class MainWindow : Window, INotifyPropertyChanged
  {
    private DataContext _data = new DataContext();
    internal DataContext Data { get => _data ?? new DataContext(); set => _data = value; }

    public MainWindow()
    {
      InitializeComponent();
      DataContext = POProperties.Instance;
    }    

    public void ClearFocus_OnClick(object sender, MouseButtonEventArgs e) { Keyboard.ClearFocus(); }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string Name = "")
    { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name)); }
  }
}

POProperties代码:

namespace POSystem
{
  public class POProperties : INotifyPropertyChanged
  {
    public POProperties() { }

    private static readonly POProperties instance = new POProperties();
    public static POProperties Instance { get => instance; }

    private UserInformation uinfo = new UserInformation();
    public UserInformation GetUserInformation { get => uinfo; }

    private Cursor _ltc_cursor;
    public Cursor LTC_Cursor
    {
      get => _ltc_cursor ?? new Cursor(new MemoryStream(Properties.Resources.LTC_Custom_Cursor));
      set => _ltc_cursor = value;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string Name = "")
    { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name)); }
}

这个方法我不再从绑定中得到任何错误,而且效果很好。我知道这个方法对这个问题更为重要,但我认为静态类方法会更好。我显然错了。