“Canvas” 方法在 itemsControl 中不起作用。

huangapple go评论73阅读模式
英文:

"Canvas" methods doesn't work in itemsControl

问题

Canvas 方法通常在 canvas 标签中使用时可以正常工作,但当您尝试在 ItemsControl 中使用它们时,有时会停止工作。以下是一个示例:

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Click="Button_Click">Text</Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

xaml.cs 文件:

private void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show(Canvas.GetTop(sender as Button).ToString());
}

在这里,消息框返回 NaN,我不知道原因。您也不能使用 SetTop 或 left 方法。您能帮忙吗?

编辑: 这里有另一件有趣的事情
如果我给元素命名,它确实可以工作,但我不想这样做,因为名称在 itemTemplate 中不可用。

以下是代码:

<ItemsControl ItemsSource="{Binding SomeList}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <Button x:Name="Btn" Click="Button_Click">Text</Button>
</ItemsControl>

xaml.cs 文件:

private void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show(Canvas.GetTop(Btn).ToString());
}
英文:

Canvas methods always work when they are in a canvas tag, but When you tryna use them in an itemsControl, they sometimes stop working. here's an example:

&lt;ItemsControl&gt;
        &lt;ItemsControl.ItemsPanel&gt;
            &lt;ItemsPanelTemplate&gt;
                &lt;Canvas /&gt;
            &lt;/ItemsPanelTemplate&gt;
        &lt;/ItemsControl.ItemsPanel&gt;
    &lt;ItemsControl.ItemTemplate&gt;
        &lt;DataTemplate&gt;
            &lt;Button Click=&quot;Button_Click&quot;&gt;Text&lt;/Button&gt;
        &lt;/DataTemplate&gt;
    &lt;/ItemsControl.ItemTemplate&gt;
   
    &lt;/ItemsControl&gt;

xaml.cs file

private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Canvas.GetTop(sender as Button).ToString());
    }

Here messagebox returns Nan and I don't know the reason. You can't use SetTop or left method either. Can you help?

Edit: Here's another interesting thing
if I give a name to an element it does work, but I don't want that because the names aren't available in the itemTemplate.

here's the code:

 &lt;ItemsControl ItemsSource=&quot;{Binding SomeList}&quot;&gt;
        &lt;ItemsControl.ItemsPanel&gt;
            &lt;ItemsPanelTemplate&gt;
                &lt;Canvas /&gt;
            &lt;/ItemsPanelTemplate&gt;
        &lt;/ItemsControl.ItemsPanel&gt;
   &lt;Button x:Name=&quot;Btn&quot; Click=&quot;Button_Click&quot;&gt;Text&lt;/Button&gt;
   
    &lt;/ItemsControl&gt;

xaml.cs file

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Canvas.GetTop(Btn).ToString());
    }

答案1

得分: 1

为了定义ItemTemplate中元素的绝对位置,您应该将容器的Canvas.Top附加属性绑定到SomeList集合中对象类型的属性:

<ItemsControl ItemsSource="{Binding SomeList}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Top" Value="{Binding Top}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Click="Button_Click">Text</Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

SomeList应该是一个IEnumerable<T>,其中类型T具有double类型的Top属性:

public class Item
{
    public double Top { get; set; }
}
英文:

To position define the absolute position of the element in the ItemTemplate, you should bind the Canvas.Top attached property of the container to a property of the type of objects in the SomeList collection:

&lt;ItemsControl ItemsSource=&quot;{Binding SomeList}&quot;&gt;
    &lt;ItemsControl.ItemsPanel&gt;
        &lt;ItemsPanelTemplate&gt;
            &lt;Canvas /&gt;
        &lt;/ItemsPanelTemplate&gt;
    &lt;/ItemsControl.ItemsPanel&gt;
    &lt;ItemsControl.ItemContainerStyle&gt;
        &lt;Style TargetType=&quot;ContentPresenter&quot;&gt;
            &lt;Setter Property=&quot;Canvas.Top&quot; Value=&quot;{Binding Top}&quot; /&gt;
        &lt;/Style&gt;
    &lt;/ItemsControl.ItemContainerStyle&gt;
    &lt;ItemsControl.ItemTemplate&gt;
        &lt;DataTemplate&gt;
            &lt;Button Click=&quot;Button_Click&quot;&gt;Text&lt;/Button&gt;
        &lt;/DataTemplate&gt;
    &lt;/ItemsControl.ItemTemplate&gt;
&lt;/ItemsControl&gt;

SomeList should be an IEnumerable&lt;T&gt; where the type T has a Top property of type double:

public class Item
{
    public double Top { get; set; }
}

huangapple
  • 本文由 发表于 2023年6月27日 21:49:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定