小编scu*_*a88的帖子

GridViewColumn CellTemplate中每个DataType的DataTemplate

我有一个ObservableCollection,它包含多种类型的视图模型,我想为每个GridViewColumn的CellTemplates中的每个类型创建一个DataTemplate.在这个简单的例子中,我可以创建一个基本的ViewModel,但我希望能够从xaml中完成这个.下面的xaml显示了我想要做的事情,其中​​一个DataTemplates将用于每个CellTemplate.

如果有一个GridViewColumn.Resources我会在那里定义DataTemplates然后在CellTemplate中使用DataTemplate和ContentPresenter,但我显然不能这样做.我想我可能需要一个TemplateSelector,但我不知道从哪里开始.

<ListView ItemsSource={Binding GenericObservableCollection>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Type">
                <GridViewColumn.CellTemplate>
                    <DataTemplate DataType="{x:Type vm:ActionInputViewModel}">
                        <TextBlock Text="Input"/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type vm:ActionOutputViewModel}">
                        <TextBlock Text="Output"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Value">
                <GridViewColumn.CellTemplate>
                    <DataTemplate DataType="{x:Type vm:ActionInputViewModel}">
                        <TextBlock Text="{Binding Property1}"/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type vm:ActionOutputViewModel}">
                        <TextBlock Text="{Binding Property2}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)

wpf datatemplate gridviewcolumn celltemplate

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

如何修复AttachedBehavior上的DependencyPropertyDescriptor AddValueChanged内存泄漏?

我知道我需要调用RemoveValueChanged,但我找不到一个可靠的地方来调用它.我知道可能没有一个.

我看起来需要找到一种不同的方法来监控更改,然后使用AddValueChanged添加一个处理程序.我正在寻找有关实现这一目标的最佳方法的建议.我已经看到了在PropertyMetadata中使用PropertyChangedCallback的建议,但是当我的TextBox和Adorner不是静态的时候我不确定如何做到这一点.此外,IsFocused属性不是在我的类中创建的DependencyProperty.

谢谢.

public sealed class WatermarkTextBoxBehavior
{
    private readonly TextBox m_TextBox;
    private TextBlockAdorner m_TextBlockAdorner;

    private WatermarkTextBoxBehavior(TextBox textBox)
    {
        if (textBox == null)
            throw new ArgumentNullException("textBox");

        m_TextBox = textBox;
    }

    #region Behavior Internals

    private static WatermarkTextBoxBehavior GetWatermarkTextBoxBehavior(DependencyObject obj)
    {
        return (WatermarkTextBoxBehavior)obj.GetValue(WatermarkTextBoxBehaviorProperty);
    }

    private static void SetWatermarkTextBoxBehavior(DependencyObject obj, WatermarkTextBoxBehavior value)
    {
        obj.SetValue(WatermarkTextBoxBehaviorProperty, value);
    }

    private static readonly DependencyProperty WatermarkTextBoxBehaviorProperty =
        DependencyProperty.RegisterAttached("WatermarkTextBoxBehavior",
            typeof(WatermarkTextBoxBehavior), typeof(WatermarkTextBoxBehavior), new UIPropertyMetadata(null));

    public static bool GetEnableWatermark(TextBox obj)
    {
        return (bool)obj.GetValue(EnableWatermarkProperty);
    }

    public static void SetEnableWatermark(TextBox obj, bool value) …
Run Code Online (Sandbox Code Playgroud)

wpf memory-leaks dependency-properties attachedbehaviors

9
推荐指数
2
解决办法
7336
查看次数