小编Cad*_*ris的帖子

具有绑定依赖属性的IValueConverter

我需要根据要绑定的对象中标识的单元系统确定运行时StringFormat的某些TextBlocks绑定.

我有一个具有依赖属性的转换器,我想绑定.Bound值用于确定转换过程.

public class UnitConverter : DependencyObject, IValueConverter
{
    public static readonly DependencyProperty IsMetricProperty =
        DependencyProperty.Register("IsMetric", typeof(bool), typeof(UnitConverter), new PropertyMetadata(true, ValueChanged));

    private static void ValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        ((UnitConverter)source).IsMetric = (bool)e.NewValue;
    }

    public bool IsMetric
    {
        get { return (bool)this.GetValue(IsMetricProperty); }
        set { this.SetValue(IsMetricProperty, value); }
    }

    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (IsMetric)
            return string.Format("{0:0.0}", value);
        else
            return string.Format("{0:0.000}", value);
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf ivalueconverter

9
推荐指数
1
解决办法
5529
查看次数

ReadOnlyCollections的Xml序列化

我有一些容器类通过ReadOnlyCollection公开它们的集合.自定义方法提供给集合中的添加和删除,它还​​执行一些自定义逻辑.

例如:

public class Foo
{
    List<Bar> _barList = new List<Bar>();

    public ReadOnlyCollection<Bar> BarList
    {
        get { return _barList.AsReadOnly(); }
    }

    public void AddBar(Bar bar)
    {
        if (bar.Value > 10)
            _barList.Add(bar);
        else
            MessageBox.Show("Cannot add to Foo. The value of Bar is too high");
    }
    public void RemoveBar(Bar bar)
    {
        _barList.Remove(bar);
        // Foo.DoSomeOtherStuff();
    }

}

public class Bar
{
    public string Name { get; set; }
    public int Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这一切都很好但是当我使用Xml Serializer序列化Foo时会抛出异常.

任何人都可以提供一个好方法来解决这个问题吗?

谢谢

c#

8
推荐指数
1
解决办法
2775
查看次数

更改wpf静态资源的值

如何在运行时更改WPF静态资源的值?

我有以下资源

<UserControl.Resources>
    <sys:String x:Key="LengthFormat">#.# mm</sys:String>
    <sys:String x:Key="AreaFormat">#.# mm²</sys:String>
    <sys:String x:Key="InertiaFormat">#.# mm?</sys:String>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

一些文字块参考

<TextBlock Grid.Row="2" Grid.Column="1" 
 Text="{Binding Path=Breadth, StringFormat={StaticResource ResourceKey=LengthFormat}}" />
Run Code Online (Sandbox Code Playgroud)

然后根据要绑定到控件的对象,我想改变格式.我在控件中设置了如下属性:

public string LengthFormat
{
    set
    {
        this.Resources["LengthFormat"] = value;
    }
}
public string AreaFormat
{
    set
    {
        this.Resources["AreaFormat"] = value;
    }
}
public string InertiaFormat
{
    set
    {
        this.Resources["InertiaFormat"] = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在绑定之前我设置每个字符串.

但它不起作用,有人建议whynot?

干杯

c# wpf resourcedictionary

6
推荐指数
1
解决办法
1万
查看次数

在vb.net中引用未安装的对象

嗨,我不得不学习VB.net以获得一份以前曾经是C#的新工作.我刚刚遇到了VB.net的一个有趣的功能.我可以引用尚未实例化的第二个表单上的对象!

所以从Form1我可以在Form2上获得textbox1的text属性,如下所示

Dim txt As String = Form2.TextBox1.Text
Run Code Online (Sandbox Code Playgroud)

谁能解释一下这是如何工作的?是否所有表格都在程序开始时进行了实例化,然后在程序生命周期内切换了它们的可见性?

vb.net

5
推荐指数
1
解决办法
68
查看次数