Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 双向绑定MySettings到DataGrid列宽_Wpf_Vb.net_Xaml_Binding_Application Settings - Fatal编程技术网

Wpf 双向绑定MySettings到DataGrid列宽

Wpf 双向绑定MySettings到DataGrid列宽,wpf,vb.net,xaml,binding,application-settings,Wpf,Vb.net,Xaml,Binding,Application Settings,我正在尝试将DataGrid中的列宽度绑定到应用程序设置属性。我有这个工作时,绑定设置为单向模式,但是,我需要设置更新的基础上的宽度列时,应用程序关闭。当我将绑定模式更改为双向时,绑定会一起中断。我的代码如下,我如何才能实现这一点 扩展类 Public Class SettingBindingExtension Inherits Binding Public Sub New() Initialize() End Sub Public Sub

我正在尝试将DataGrid中的列宽度绑定到应用程序设置属性。我有这个工作时,绑定设置为单向模式,但是,我需要设置更新的基础上的宽度列时,应用程序关闭。当我将绑定模式更改为双向时,绑定会一起中断。我的代码如下,我如何才能实现这一点

扩展类

Public Class SettingBindingExtension
    Inherits Binding

    Public Sub New()
        Initialize()
    End Sub

    Public Sub New(ByVal path As String)
        MyBase.New(path)
        Initialize()
    End Sub

    Private Sub Initialize()
        Me.Source = MySettings.[Default]

        'OneWay mode works for the initial grid load but any resizes are unsaved.
        Me.Mode = BindingMode.OneWay

        'using TwoWay mode below breaks the binding...
        'Me.Mode = BindingMode.TwoWay
    End Sub

End Class
xaml

xmlns:w="clr-namespace:Stack"

<DataGrid>
...
    <DataGridTextColumn Header="STACK" 
                        Width="{w:SettingBinding StackColumnWidth}"/>
...
</DataGrid>
xmlns:w=“clr命名空间:堆栈”
...
...

DataGridTextColumn.Width属性肯定可以处理
双向绑定
,因此我只能假设您的自定义
绑定
对象导致了此问题。你说
绑定
坏了,但你没有告诉我们错误是什么。作为一个简单的测试,尝试用标准的
绑定
类替换它:

<DataGridTextColumn Header="STACK" Width="{Binding StackColumnWidth}" />

问题在于宽度的类型为DataGridLength,并且没有返回到double的默认转换器,因此您需要创建自己的转换器来完成此操作,下面是一个转换器示例,它应该可以工作:

class LengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridLengthConverter converter=new DataGridLengthConverter();
        var res = converter.ConvertFrom(value);
        return res;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridLength length = (DataGridLength)value ;
        return length.DisplayValue;
    }
}

感谢您的回复,这是一个数据类型问题。我没有使用转换器,而是将设置的数据类型改为
DataGridLength
。其他一切都没有改变,一切都正常运转。再次感谢


感谢您提供的绝妙解决方案
class LengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridLengthConverter converter=new DataGridLengthConverter();
        var res = converter.ConvertFrom(value);
        return res;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridLength length = (DataGridLength)value ;
        return length.DisplayValue;
    }
}